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.