如何使用重命名一个水桶内的文件夹中的AmazonS3

如何使用重命名一个水桶内的文件夹中的AmazonS3

本文介绍了如何使用重命名一个水桶内的文件夹中的AmazonS3.net SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,S3并没有真正支持文件夹的概念。复制/删除所有文件应该工作。但也许有人知道一个简单的方法来做到这一点(我只是还没有找到它诠释了SDK)

更新/解决方案:

下面code为我工作。

 使用(AmazonS3客户端= Amazon.AWSClientFactory.CreateAmazonS3Client(XXX,
                                                              YYY))
        {

            ListObjectsRequest listRequest =新ListObjectsRequest();
            listRequest.WithBucketName(ytimusic)
                。以preFIX(M+ oldOWnerId.ToString()+/);


            //获取所有对象内部的文件夹
            ListObjectsResponse对象= client.ListObjects(listRequest);
            的foreach(S3Object S3O在objects.S3Objects)
            {

                //获取对象的访问控制列表
                GetACLRequest aclRequest =新GetACLRequest();
                aclRequest.WithBucketName(thebucket)
                    .WithKey(s3o.Key);
                GetACLResponse getAclResponse = client.GetACL(aclRequest);


                //复制对象没有ACL
                字符串则newkey = s3o.Key.Replace(oldOWnerId.ToString(),newOwnerId.ToString());
                CopyObjectRequest copyRequest =新CopyObjectRequest();
                copyRequest.SourceBucket =thebucket;
                copyRequest.DestinationBucket =thebucket;
                copyRequest.WithSourceKey(s3o.Key)
                .WithDestinationKey(则newkey);
                S3Response copyResponse = client.CopyObject(copyRequest);

                //设置新创建的对象的访问控制列表
                SetACLRequest setAclRequest =新SetACLRequest();
                setAclRequest.WithBucketName(ytimusic)
                    .WithKey(则newkey)
                    .WithACL(getAclResponse.AccessControlList);

                SetACLResponse setAclRespone = client.SetACL(setAclRequest);

                DeleteObjectRequest deleteRequest =新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)的复制操作不会preserve的访问控制列表,因此,如果移动的文件都是公开的,你需要明确地标记副本市民为好。

I know that s3 does not really support the folder concept. Copy/Delete all files should work. But maybe somebody knows a simpler way to do this (and I just haven't found it int the SDK)

Update/Solution:

The following code is working for me.

       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 doesn't support neither renaming nor folders, so you actually need to process each file individually by creating a copy with the desired name and deleting the original. To do this with the .NET SDK I'm afraid there is no easier way than listing all files in the directory, and renaming each using CopyObject+DeleteObject. Also, be aware that according to the docs (http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectCOPY.html) the copy operation does not preserve the ACL, so if the moved files are public you need to explicitly mark the copies as public as well.

这篇关于如何使用重命名一个水桶内的文件夹中的AmazonS3.net SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:18