Home > Blockchain >  Move S3 objects to another folder in same bucket but with same permissions using C#
Move S3 objects to another folder in same bucket but with same permissions using C#

Time:02-16

I am able to move an object in an S3 bucket from one directory to another directory using C# but unable to copy all current permissions with that object.

For example, my current object has public access permissions but after moving it to another directory it lost the public read permissions.

Here is the code I'm using to move objects:

public void MoveFile(string sourceBucket, string destinationFolder, string file) {
        AmazonS3Client s3Client = new AmazonS3Client(ConfigurationHelper.AmazonS3AccessKey, ConfigurationHelper.AmazonS3SecretAccessKey, Amazon.RegionEndpoint.USEast1);

        S3FileInfo currentObject = new S3FileInfo(s3Client, sourceBucket, file);
        currentObject.MoveTo(sourceBucket   "/"   destinationFolder, file);
    }

Here is the output after moving file to another directory:

It lost public "Read" permission.

CodePudding user response:

I've figure out issue myself, by using CopyObject() and DeleteObject() instead of using moveTo inbuild method and that solves my issue,

here is the code which really helped me:

CopyObjectRequest copyObjectRequest = new CopyObjectRequest
        {
            SourceBucket = sourceBucket,
            DestinationBucket = sourceBucket   "/"   destinationFolder,
            SourceKey = file,
            DestinationKey = file,
            CannedACL = S3CannedACL.PublicRead,
            StorageClass = S3StorageClass.StandardInfrequentAccess,
        };

        CopyObjectResponse response1 = s3Client.CopyObject(copyObjectRequest);

        var deleteObjectRequest = new DeleteObjectRequest
        {
            BucketName = sourceBucket,
            Key = file
        };

        s3Client.DeleteObject(deleteObjectRequest);

I'm posting answer so it can be helpful for someone!!!

  • Related