如何获取Amazon S3服务的对象键

5
我正在尝试使用Java从Amazon S3获取文件(http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html),但是由于不知道对象键是什么,所以遇到了问题。
存储桶名称为testbucket,在此存储桶中有一个名为files的文件夹。如果我有一个名为image.jpeg的文件,则对象键是什么?
private static String bucketName = "testbucket"; 
private static String key="files/image.jpeg";      


public static void main(String[] args) throws IOException {
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
    S3Object s3object = s3Client.getObject(new GetObjectRequest(
                bucketName, key));
     } 

我得到这个输出:
  21:43:54.054 [main] DEBUG com.amazonaws.request - Sending Request: GET https://adap-demo.s3.amazonaws.com /report_templates/Testreport_1.jrxml
Headers: (User-Agent: aws-sdk-java/1.10.30 Linux/4.4.0-70-generic
OpenJDK_64-Bit_Server_VM/25.121-b13/1.8.0_121, Content-Type:
application/x-www-form-urlencoded; charset=utf-8, ) 
21:43:54.164 [main] DEBUG c.a.services.s3.internal.S3Signer - Calculated
string to sign:
"GET
application/x-www-form-urlencoded; charset=utf-8
Sun, 02 Apr 2017 19:43:54 GMT
/adap-demo/report_templates/Testreport_1.jrxml"
Caught an AmazonClientException, which means the client encountered an
internal error while trying to communicate with S3, such as not being able
to access the network.
Error Message: Unable to calculate a request signature: Unable to   
calculate a request signature: Empty key 

2
关键字应为files/image.jpeg。要检查关键字,请点击文件并查看链接。链接应为https://s3-[region]..amazonaws.com/[bucket name]/[file key] - Leon
1
无法工作并且出现了AmazonClientException错误,这意味着客户端在尝试与S3通信时遇到了内部错误,例如无法访问网络。 错误信息:无法计算请求签名:无法计算请求签名:空密钥。 - Ali-Alrabi
1
显然,你正在将一个空键传递给函数以检索对象。请问你能否将用于获取对象的代码添加到问题中? - Leon
2
代码对我来说看起来是正确的。你的AWS凭证是否已经放置在凭证提供者(文件~/.aws/credentials应该包含你的凭证)中?另外,如果你只想提供对象键和存储桶名称,getObject有一个简写方式。 - Leon
1
@Leon 谢谢你的帮助,这是一个凭证问题。 - Ali-Alrabi
1个回答

0
简短回答:在您的特定情况下,对象键是files/image.jpg
从技术角度来看,Amazon S3是一个简单的基于键的对象存储。当您存储数据时,您会分配一个唯一的对象键,以便以后可以用它来检索数据。S3的数据模型是一个扁平结构:您创建一个存储桶,而存储桶存储对象。没有“子存储桶”或子文件夹的层次结构。但是,您可以使用键名前缀推断逻辑层次结构。
在您的情况下,前缀是files/,对象名称是image.jpg。键是前缀与对象名称连接起来的结果。
例如,传统的基于文件的目录结构如下所示:
- files
    - index.html
    - images
        - logo.png
        - photo.jpg

将其翻译为以下在S3上的扁平结构,其中每行代表相应对象的
files/index.html
files/images/logo.png
files/images/photo.jpg

你可以在这里了解更多关于Amazon S3对象键的创建和使用:https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接