本文介绍了Amazon S3 复制对象 Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从一个 s3 文件夹复制到同一存储桶中的另一个文件夹.我正在使用 AmazonS3 类中的 copyObject 函数.我没有看到任何错误或异常,我也得到了结果.但是文件没有被复制.如果有任何失败,我至少会排除一些错误.我做错了什么?我怎么知道实际错误?

I am trying to copy data from one s3 folder to another within same bucket. I am using copyObject function from AmazonS3 class. I don't see any errors or exceptions and I do get result also. But the file is not copied. I would at least except some error if there is any failure. What I am doing wrong?How do I know the actual error?

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());CopyObjectRequest copyObjRequest = new CopyObjectRequest(sourceURI.getBucket(), sourceURI.getKey(), destinationURI.getBucket(), destinationURI.getKey());

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());CopyObjectRequest copyObjRequest = new CopyObjectRequest( sourceURI.getBucket(), sourceURI.getKey(), destinationURI.getBucket(), destinationURI.getKey());

CopyObjectResult copyResult = s3client.copyObject(copyObjRequest);

CopyObjectResult copyResult = s3client.copyObject(copyObjRequest);

我在源和目标 URI 中有正确的值.是因为证件吗?如果由于缺少凭据而无法正常工作,我希望此代码会出错.

I have proper values in source and destination URI. Is it because of credentials? If in case it is not working because of missing credentials I expect an error from this code.

推荐答案

我怀疑您错误地指定了副本的目的地.

I suspect that you have incorrectly specified the destination of the copy.

目标是一个完整的存储桶和键,例如,如果您将 dogs/beagle.png 复制到 smalldogs/beagle.png,那么将目标键指定为 smalldogs/是不够的.这就是副本在 NTFS 或 NFS 等常规文件系统上的工作方式,而不是它们在对象存储中的工作方式.这将产生一个名为 smalldogs/的对象,它看起来是一个文件夹,但实际上是比格犬图像的副本.

The destination is a complete bucket and key, for example if you are copying dogs/beagle.png to smalldogs/beagle.png then it is not sufficient to specify the destination key as smalldogs/. That's how copies work on a regular file system like NTFS or NFS, but not how they work in object storage. What that will result in is an object named smalldogs/ and it will appear to be a folder, but it's actually a copy of the beagle image.

因此,删除您创建的错误对象,然后完全正确地指定目标.

So, delete the errant object that you created and then specify the destination fully and correctly.

注意这里的操作是copy.如果你想移动",那么你需要在之后删除源.

Note that the operation here is copy. If you want 'move' then you need to delete the source afterwards.

以下是一些基于您的代码,它们对我来说运行良好,并生成了正确的复制文件:

Here is some code based on yours that runs fine for me and results in the correct copied file:

String bkt = "my-bucket";
String src = "dogs/beagle.png";
String dst = "smalldogs/beagle.png";

AmazonS3Client s3 = new AmazonS3Client();
CopyObjectRequest req = new CopyObjectRequest(bkt, src, bkt, dst);
CopyObjectResult res = s3.copyObject(req);

这篇关于Amazon S3 复制对象 Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:02