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

查看:110
本文介绍了如何使用 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 通常不包含为公共可读对象创建 URL 的便捷方法.但是,当您调用 PutObject 时, 提供了存储桶和对象的键,这就是您所需要的.您可以简单地将它们组合起来制作对象的 URL,例如:

The SDKs do not generally contain a convenience method to create a URL for publicly-readable objects. However, when you called PutObject, you provided the bucket and the object's key and that's all you need. 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:

请注意,S3 密钥不以 / 前缀开头.密钥的格式为 dogs/toto.png,而不是 /dogs/toto.png.

Note that S3 keys do not begin with a / prefix. A key is of the form dogs/toto.png, and not /dogs/toto.png.

对于特定于区域的存储桶,请参阅使用 Amazon S3 存储桶AWS S3 URL 样式.将上述 URL 中的 s3 替换为 s3..amazonaws.coms3-.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 (with dot)
  • https://seamus.s3-eu-west-1.amazonaws.com/dogs/setter.png (with dash)

如果您使用的是 IPv6,则一般 URL 格式为:

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

对于某些存储桶,您可以使用较旧的路径样式 URL.不推荐使用路径样式的 URL 并且 仅适用于 2020 年 9 月 30 日或之前创建的存储分区.它们是这样使用的:

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:

目前存在 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.

注意:关于对象键 某些字符需要特殊处理.例如,空格编码为 +(加号),加号编码为 %2B.还有 这里.

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天全站免登陆