如何使用aws-sdk在AWS S3上获取文件的URL? [英] How to get the URL of a file on AWS S3 using aws-sdk?

查看:1271
本文介绍了如何使用aws-sdk在AWS S3上获取文件的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 putObject method, I get a data object, but it only contains an Etag of the recently added object.

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

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

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.

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

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

谢谢!

推荐答案

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

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:

例如,如果您的存储桶是 pablo ,而对象键是 dogs/toto.png ,请使用:

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

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

For region-specific buckets, see Working with Amazon S3 Buckets and AWS S3 URL Styles. Replace s3 with s3.<region>.amazonaws.com or s3-<region>.amazonaws.com in the above URLs, for example:

  • https://seamus.s3.eu-west-1.amazonaws.com/dogs/setter.png
  • https://seamus.s3-eu-west-1.amazonaws.com/dogs/setter.png

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

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

对于某些存储桶,您可以使用较旧的路径样式URL.不建议使用路径样式的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:

  • https://s3.amazonaws.com/bucket/key
  • https://s3.amazonaws.com/pablo/dogs/toto.png
  • https://s3.eu-west-1.amazonaws.com/seamus/dogs/setter.png
  • https://s3.dualstack.REGION.amazonaws.com/BUCKET

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

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.

注意:有关此处.

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

这篇关于如何使用aws-sdk在AWS S3上获取文件的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆