node.js 如何使用 aws-sdk 在 AWS S3 上获取文件的 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44400227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:16:36  来源:igfitidea点击:

How to get the URL of a file on AWS S3 using aws-sdk?

node.jsamazon-web-servicesamazon-s3

提问by Pablo Quemé

I'm using an AWS Lambda function to create a file and save it to my bucket on S3, it is working fine. After executing the putObjectmethod, I get a dataobject, but it only contains an Etagof the recently added object.

我正在使用 AWS Lambda 函数创建一个文件并将其保存到 S3 上的存储桶中,它工作正常。执行该putObject方法后,我得到一个data对象,但它只包含一个Etag最近添加的对象。

s3.putObject(params, function(err, data) {
    // data only contains Etag
});

I need to know the exact URL that I can use in a browser so the client can see the file. The folder has been already made public and I can see the file if I copy the Link from the S3 console.

我需要知道我可以在浏览器中使用的确切 URL,以便客户端可以看到该文件。该文件夹已公开,如果我从 S3 控制台复制链接,我可以看到该文件。

I tried using getSignedUrlbut the URL it returns is used for other purposes, I believe.

我尝试使用,getSignedUrl但它返回的 URL 用于其他目的,我相信。

Thanks!

谢谢!

回答by jarmod

The SDKs do not contain a convenience method to do this. However, when you called PutObject, you provided the bucket and the object's key. You can simply combine those to make the URL of the object, for example:

SDK 不包含执行此操作的便捷方法。但是,当您调用 PutObject 时,您提供了存储桶和对象的键。您可以简单地将它们组合起来制作对象的 URL,例如:

So, for example, if your bucket is pabloand the object key is dogs/toto.png, use:

因此,例如,如果您的存储桶是pablo并且对象键是Dogs/toto.png,请使用:

For region-specific buckets, see Working with Amazon S3 Bucketsand AWS S3 URL Styles. Replace s3with s3.<region>.amazonaws.comor s3-<region>.amazonaws.comin the above URLs, for example:

对于特定于区域的存储桶,请参阅使用 Amazon S3 存储桶AWS S3 URL 样式。替换s3s3.<region>.amazonaws.coms3-<region>.amazonaws.com在上述 URL 中,例如:

If you are using IPv6, then the general URL form will be one of:

如果您使用的是 IPv6,则一般 URL 形式将是以下之一:

For some buckets, you may use the older path-style URLs. Path-style URLs are deprecated and only work with buckets created on or before September 30, 2020. They are used like this:

对于某些存储桶,您可以使用较旧的路径样式 URL。路径样式 URL 已弃用,仅适用于 2020 年 9 月 30 日或之前创建的存储桶。它们是这样使用的:

Currently there are TLS and SSL certificate issues that may require some buckets with dots (.) in their name to be accessed via path-style URLs. AWS plans to address this. See the AWS announcement.

当前存在 TLS 和 SSL 证书问题,可能需要.通过路径样式 URL 访问名称中带有点 ( ) 的某些存储桶。AWS 计划解决这个问题。请参阅AWS 公告

Note:General guidance on object keyswhere certain characters need special handling. For example space is encoded to + (plus sign) and plus sign is encoded to %2B. Also here.

注意:关于某些字符需要特殊处理的对象键的一般指南。例如,空格被编码为 +(加号),加号被编码为 %2B。也在这里

回答by Helio Albano

You can do an another call with this:

你可以用这个做另一个电话:

var params = {Bucket: 'bucket', Key: 'key'};
s3.getSignedUrl('putObject', params, function (err, url) {
  console.log('The URL is', url);
});

回答by adir abargil

in case you got the s3bucket and filename objects and want to extract the url, here is an option:

如果您有 s3bucket 和 filename 对象并想提取 url,这里有一个选项:

const getUrlFromBucket=(s3Bucket,fileName)=>{
    return 'https://'+s3Bucket.config.params.Bucket+'.s3-' 
            + s3Bucket.config.region+'.amazonaws.com/'+fileName
};