如何使用AmazonS3.net sdk重命名存储桶中的文件夹

我知道s3并不真正支持文件夹概念.复制/删除所有文件应该工作.但也许有人知道一种更简单的方法(我只是没有在SDK中找到它)

更新/解决方案:

以下代码对我有用.

       using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("xxx",
                                                              "yyy"))
        {

            ListObjectsRequest listRequest = new ListObjectsRequest();
            listRequest.WithBucketName("ytimusic")
                .WithPrefix("M" + oldOWnerId.ToString()+"/");


            // get all objects inside the "folder"
            ListObjectsResponse objects = client.ListObjects(listRequest);
            foreach (S3Object s3o in objects.S3Objects)
            {

                // get the acl of the object
                GetACLRequest aclRequest = new GetACLRequest();
                aclRequest.WithBucketName("thebucket")
                    .WithKey(s3o.Key);
                GetACLResponse getAclResponse = client.GetACL(aclRequest);


                // copy the object without acl
                string newKey = s3o.Key.Replace(oldOWnerId.ToString(), newOwnerId.ToString());
                CopyObjectRequest copyRequest = new CopyObjectRequest();
                copyRequest.SourceBucket = "thebucket";
                copyRequest.DestinationBucket = "thebucket";
                copyRequest.WithSourceKey(s3o.Key)
                .WithDestinationKey(newKey);
                S3Response copyResponse = client.CopyObject(copyRequest);

                // set the acl of the newly created object
                SetACLRequest setAclRequest = new SetACLRequest();
                setAclRequest.WithBucketName("ytimusic")
                    .WithKey(newKey)
                    .WithACL(getAclResponse.AccessControlList);

                SetACLResponse setAclRespone = client.SetACL(setAclRequest);

                DeleteObjectRequest deleteRequest = new DeleteObjectRequest();
                deleteRequest.WithBucketName("thebucket")
                    .WithKey(s3o.Key);

                DeleteObjectResponse deleteResponse = client.DeleteObject(deleteRequest);

            }
        }
最佳答案
S3既不支持重命名也不支持文件夹,因此您实际上需要通过创建具有所需名称的副本并删除原始文件来单独处理每个文件.要使用.NET SDK执行此操作,我担心没有比列出目录中的所有文件更简单的方法,并使用CopyObject DeleteObject重命名每个文件.另外,请注意,根据文档(http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectCOPY.html),复制操作不会保留ACL,因此如果移动的文件是公开的您还需要将副本明确标记为公开.

转载注明原文:如何使用AmazonS3.net sdk重命名存储桶中的文件夹 - 代码日志