我成功获取了存储桶的内容,但我无法弄清楚如何在存储桶中存在特定文件时询问S3(我不想下载整个文件列表并检查客户端,因为也有很多文件).
供参考:AWS2JS documentation on S3.GET
var s3 = require('aws2js').load('s3', appConfig.awsAccessKeyId, appConfig.awsSecretAccessKey); s3.setBucket(appConfig.awsBucketName); var folder = encodeURI('reports'); var url = '?prefix=' + folder; s3.get(url,{Contents: null, Key: 'reports/emot_cat_r1180341142.png'},'xml', function (error, data) { console.log("Error",error); console.log("Data",data); true.should.be.true; done(); });
输出
{ Name: ‘umusergen’, Prefix: ‘reports’, Marker: {}, MaxKeys:
‘1000’, IsTruncated: ‘false’, Contents: [ { Key: ‘reports/’,
LastModified: ‘2013-06-16T17:44:25.000Z’,
ETag: ‘”d41d8cd98f00b204e9800998ecf8427e”‘,
Size: ‘0’,
Owner: [Object],
StorageClass: ‘STANDARD’ },
{ Key: ‘reports/emot_cat_r1180341142.png’,
LastModified: ‘2013-06-16T23:18:59.000Z’,
ETag: ‘”0b04aa9146d36a447019b1aa94be7eb3″‘,
Size: ‘26374’,
Owner: [Object],
StorageClass: ‘STANDARD’ },
{ Key: ‘reports/polarity_r1180341142.png’,
LastModified: ‘2013-06-16T23:19:00.000Z’,
ETag: ‘”22fd521e0a5e858040082d0d47078532″‘,
Size: ‘23091’,
Owner: [Object],
StorageClass: ‘STANDARD’ },
{ Key: ‘reports/wordcloud_r1180341142.png’,
LastModified: ‘2013-06-16T23:19:07.000Z’,
ETag: ‘”9f7ee9d2fdce5f460b2c42444edd6efc”‘,
Size: ‘167164’,
Owner: [Object],
StorageClass: ‘STANDARD’ } ], ‘@’: { xmlns: { xmlns: ‘07001’ } } }
“{Contents:null,Key:'[file path / name]’}”是我尝试解释上面引用的API指令.
如您所见,它只是枚举存储桶的内容,但我需要API调用来检查特定文件是否存在.
有人可以帮忙吗?我对这一切都很陌生.
您必须使用HEAD请求以HTTP方式执行此操作.不存在的文件会触发404错误.这是一个简单的实现:var s3 = require('aws2js').load('s3', process.env.AWS_ACCEESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY) s3.setBucket(process.env.AWS2JS_S3_BUCKET) s3.head(process.argv[2], function (err, res) { if (err) { console.log(err) return } console.log(res) })
出于测试目的,请使用以下命令调用它:
node s3-check.js file/path.foo
例子:
node s3-check.js foo1.png { 'x-amz-id-2': 'BU8rLC35oZdNLh4TkE9Y5+czR5r9hg7kl/EbhkxUF+cA94F41knI2YNs/YG1acQg', 'x-amz-request-id': '7714B364EC1A27B2', date: 'Mon, 17 Jun 2013 06:42:52 GMT', 'last-modified': 'Tue, 28 May 2013 13:18:12 GMT', etag: '"2830931876c37237ae98458a99e86d85"', 'accept-ranges': 'bytes', 'content-type': 'image/png', 'content-length': '1165', server: 'AmazonS3' } node s3-check.js foo0.png { [Error: API error with HTTP Code: 404] headers: { 'x-amz-request-id': '96841B9C0BC5E66D', 'x-amz-id-2': 'ZMZH9bkrR6nhDkWK1hM+qm0dlzBOYhOZhVyT3nKlMuZgPag//5EhfBuAHZq+9ZRm', 'content-type': 'application/xml', 'transfer-encoding': 'chunked', date: 'Mon, 17 Jun 2013 06:43:53 GMT', server: 'AmazonS3' }, code: 404 }