简体   繁体   English

AWS S3 nodejs - 如何通过他的前缀获取对象

[英]AWS S3 nodejs - How to get object by his prefix

I'm searching how check if an object exist in my aws s3 bucket in nodejs without list all my object (~1500) and check the prefix of the object but I cannot find how.我正在搜索如何检查 nodejs 中我的 aws s3 存储桶中是否存在对象,而没有列出我的所有对象(~1500)并检查对象的前缀,但我找不到方法。

The format is like that:格式是这样的:

<prefix I want to search>.<random string>/

Ex:前任:

tutturuuu.dhbsfd7z63hd7833u/

Because you don't know the entire object Key, you will need to perform a list and filter by prefix.因为您不知道整个对象 Key,您将需要执行列表并按前缀过滤。 The AWS nodejs sdk provides such a method . AWS nodejs sdk 提供了这样的方法 Here is an example:下面是一个例子:

s3.listObjectsV2({
  Bucket: 'youBucket',
  MaxKeys: 1,
  Prefix: 'tutturuuu.'
}, function(err, data) {
  if (err) throw err;

  const objectExists = data.Contents.length > 0
  console.log(objectExists);
});

Note that it is important to use MaxKeys in order to reduce network usage.请注意,使用MaxKeys以减少网络使用很重要。 If more than one object has the prefix, then you will need to return everything and decide which one you need.如果不止一个对象具有前缀,那么您将需要返回所有内容并决定您需要哪一个。

This API call will return metadata only.此 API 调用将仅返回元数据。 After you have the full key you can use getObject to retrieve the object contents.获得完整密钥后,您可以使用getObject检索对象内容。

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

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