无法在S3中将文件从一个文件夹移动到另一个文件夹

问题描述 投票:1回答:1

我想将S3文件夹中的文件移动到同一个s3 Bucket中的另一个文件夹。我尝试了下面的代码

CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName,
                                              srcFolder+"/"+Filename, bucketName, 
                                              targetFolder+"/"+Filename);
s3Client.copyObject(copyObjRequest);
DeleteObjectRequest deleteObjRequest = new DeleteObjectRequest(bucketName, 
                                                        srcFolder+"/"+Filename);
s3Client.deleteObject(deleteObjRequest);

该文件夹可能包含多个文件,我只想移动所选文件。上面的代码没有显示任何错误,但没有任何反应。任何人都可以建议我正确的解决方案。

java amazon-web-services amazon-s3 aws-lambda
1个回答
0
投票

只需运行以下代码并检查输出是什么,没有和删除,这将是一个很好的初始尝试。

还值得检查对象的ACL和存储桶策略。

这是预期的格式

CopyObjectRequest(java.lang.String sourceBucketName, java.lang.String sourceKey, java.lang.String destinationBucketName, java.lang.String destinationKey)

如果要在同一个存储桶中存储该对象的副本

CopyObjectRequest copyObjRequest = new CopyObjectRequest("myBucket", "myObject.txt", "myBucket", "myNewObject.txt");
s3Client.copyObject(copyObjRequest);

如果要在不同的存储桶中存储对象的副本

CopyObjectRequest copyObjRequest = new CopyObjectRequest("myBucket", "myObject.txt", "myOtherBucket", "myNewObject.txt");
s3Client.copyObject(copyObjRequest);

用于测试的示例代码

import java.io.IOException;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CopyObjectRequest;

public class CopyObjectSingleOperation {

    public static void main(String[] args) throws IOException {
        String clientRegion = "*** Client region ***";
        String bucketName = "*** Bucket name ***";
        String sourceKey = "*** Source object key *** ";
        String destinationKey = "*** Destination object key ***";

        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new ProfileCredentialsProvider())
                    .withRegion(clientRegion)
                    .build();

            // Copy the object into a new object in the same bucket.
            CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, sourceKey, bucketName, destinationKey);
            s3Client.copyObject(copyObjRequest);
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client  
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.