Automattic / knox

S3 Lib
MIT License
1.74k stars 285 forks source link

get url with client.putFile #238

Closed pateljatin closed 10 years ago

pateljatin commented 10 years ago

Hi,

Is there a way to get url of the file stored, such as req.url (similar to client.put), with client.putFile? client.http does not return full path that includes sub-folders...(for some odd reason).

Thanks, Jatin

domenic commented 10 years ago

I don't really understand. If you pass the same arguments to client.http as you pass to client.putFile, you should get the same URL. Especially since client.putFile uses client.http...

pateljatin commented 10 years ago

I believe you're talking about the following:

var readmeUrl = client.http('/test/Readme.md');
var userDataUrl = client.https('/user.json');

here's what I'm referencing towards the following client.put - in res.on (I can get req.url - location file was saved):

var object = { foo: "bar" };
var string = JSON.stringify(object);
var req = client.put('/test/obj.json', {
    'Content-Length': string.length
  , 'Content-Type': 'application/json'
});
req.on('response', function(res){
  if (200 == res.statusCode) {
    console.log('saved to %s', req.url);
  }
});
req.end(string);

however, in the client.putFile method:

var request = client.putFile('my.json', '/user.json', { 'x-amz-acl': 'public-read' }, {
  // Always either do something with `res` or at least call `res.resume()`.
});
request.on('response', function(res) {
if (200 == res.statusCode) {
    console.log('saved to %s', request.url);
  }
});
request.on('error', function(err) {
console.log(err);
});
request.end();

however, in the client.putFile scenario, there is no response such as request.url.

hmm...

domenic commented 10 years ago

If you passed /user.json to client.putFile, it will put it at the same URL as returned by client.http('/user.json').

pateljatin commented 10 years ago

true - and I guess if I put it in folders like /upload/files/user.json then when I do client.http('/user.json') it returns a url without proper subfolders. so, I really need to include full folder path in the request client.http('/upload/files/user.json') hmm...wish I didn't had to :)

domenic commented 10 years ago

I don't understand how you would expect it to guess the folder? What if you uploaded /1/user.json and /2/user.json and /user.json? What would client.http('/user.json') return?

You always pass in full paths to every Knox method. The URL-returning methods are not special.

pateljatin commented 10 years ago

Yep - makes sense! Thx.