简体   繁体   English

node.js亚马逊s3如何检查文件是否存在

[英]node.js Amazon s3 how to check file exists

I'm trying to figure out how to do the equivalent of fs.exists() on S3. 我试图弄清楚如何在S3上执行相当于fs.exists()的操作。

I succeed in getting the contents of the bucket, but I cannot figure out how to ask S3 if a specific file exists in the bucket (I don't want to download the entire list of files and check client-side, because there are too many files). 我成功获取了存储桶的内容,但我无法弄清楚如何在存储桶中存在特定文件时询问S3(我不想下载整个文件列表并检查客户端,因为也有很多文件)。

For reference: AWS2JS documentation on S3.GET 供参考: S3.GET上的AWS2JS文档

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();
            });

The output 输出

{ 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: ' http://s3.amazonaws.com/doc/2006-03-01/ ' } } } {姓名:'umusergen',前缀:'报告',标记:{},MaxKeys:'1000',IsTruncated:'false',内容:[{Key:'reports /',LastModified:'2013-06-16T17: 44:25.000Z',ETag:'“d41d8cd98f00b204e9800998ecf8427e”',大小:'0',所有者:[Object],StorageClass:'STANDARD'},{Key:'reports / emot_cat_r1180341142.png',LastModified:'2013-06 -16T23:18:59.000Z',ETag:'“0b04aa9146d36a447019b1aa94be7eb3”',大小:'26374',所有者:[Object],StorageClass:'STANDARD'},{Key:'reports / polarity_r1180341142.png',LastModified:' 2013-06-16T23:19:00.000Z',ETag:'“22fd521e0a5e858040082d0d47078532”',大小:'23091',所有者:[Object],StorageClass:'STANDARD'},{Key:'reports / wordcloud_r1180341142.png', LastModified:'2013-06-16T23:19:07.000Z',ETag:'“9f7ee9d2fdce5f460b2c42444edd6efc”',大小:'167164',所有者:[Object],StorageClass:'STANDARD'}],'@':{xmlns: {xmlns:' http : //s3.amazonaws.com/doc/2006-03-01/ '}}}

The "{Contents: null, Key: '[file path/name]'}" is my attempt to interpret the API instructions referenced above. “{Contents:null,Key:'[file path / name]'}”是我尝试解释上面引用的API指令。

As you can see, it just enumerates the contents of the bucket, but I need the API call to check for the existence of a specific file. 如您所见,它只是枚举存储桶的内容,但我需要API调用来检查特定文件是否存在。

Can anyone help? 有人可以帮忙吗? I'm fairly new to all this. 我对这一切都很陌生。

You have to do it the HTTP way with a HEAD request. 您必须使用HEAD请求以HTTP方式执行此操作。 A file that does not exist triggers a 404 error. 不存在的文件会触发404错误。 Here's a simple implementation: 这是一个简单的实现:

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)
})

For testing purposes, invoke it with: 出于测试目的,请使用以下命令调用它:

node s3-check.js file/path.foo

Examples: 例子:

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 }

The existing answer is a few years old. 现有答案已有几年历史。 There's a better way to do this. 有一个更好的方法来做到这一点。 By using the aws-sdk module, you're provided a nice API through which to do this. 通过使用aws-sdk模块,您可以通过它获得一个很好的API。

aws-sdk has a function called headObject to look at a file's metadata. aws-sdk有一个名为headObject的函数来查看文件的元数据。 Here's example usage 这是示例用法

import Promise from 'bluebird'
import AWS from 'aws-sdk'

AWS.config.accessKeyId = Config.Credentials.AWS.accessKeyId
AWS.config.secretAccessKey = Config.Credentials.AWS.secretAccessKey

const s3 = new AWS.S3
Promise.promisifyAll(s3)

const getFileFromS3 = async ({ Bucket, Key })=> {
  try {
    const result = await S3.headObjectAsync({ Bucket, Key })
    console.log(result)
    /* Output:
      { AcceptRanges: 'bytes',
          LastModified: 'Mon, 16 Jan 2017 07:30:19 GMT',
          ContentLength: '6',
          ETag: '"b1946ac92492d2347c6235b4d2611184"',
          ContentType: 'text/plain',
          Metadata: {} }
    */
  } catch (error) {
    if (error.statusCode === 404) console.log('File Not Found')
    console.error('Unexpected Error', error)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM