[Fixed] getting "The bucket does not allow ACLs" Error – C#

Photo of author
Written By M Ibrahim
amazon-s3 amazon-web-services c#

The Problem:

In a code for uploading an image to S3, an error message "The bucket does not allow ACLs" was encountered despite setting the bucket’s object ownership to "ACLs enabled." What is causing this error and how can it be resolved?

The Solutions:

Solution 1: Enabling ACLs in Bucket Permissions

  1. Navigate to AWS S3 Console: Access the AWS S3 console and locate the bucket you’re facing issues with.

  2. Open Bucket Details: Click on the bucket’s name to open its details page.

  3. Select Permissions Tab: In the bucket details page, navigate to the ‘Permissions’ tab.

  4. Change Object Ownership: Look for a block titled "Object Ownership" and click on the option to change it.

  5. Enable ACLs: In the "Object Ownership" dropdown, select "ACLs enabled". This option allows you to set ACLs for objects within the bucket.

  6. Save Changes: Click on ‘Save’ to apply the changes.

After enabling ACLs, you should be able to use the CannedACL property in your TransferUtilityUploadRequest to set the ACL permissions for the uploaded object. This should resolve the "The bucket does not allow ACLs" error.

Solution 2: {title}

1. Go to the S3 console and select the bucket for which you want to enable ACLs.

2. Click on the “Permissions” tab.

3. Under “Bucket owner”, click on the “Edit” button.

4. In the “Object Ownership” dialog box, select the “ACLs enabled” option and click on “Save”.

5. Now you can upload images to S3 via CLI or SDK and then visit them in your browser.

Solution 3: Providing Bucket Policy containing 's3:PutObjectAcl' and 's3:GetObjectAcl'

  • The bucket policy should include the following actions:
    "s3:GetObjectAcl" and "s3:PutObjectAcl".

  • Remove the CannedACL = S3CannedACL.PublicRead; attribute from the fileTransferUtilityRequest.

  • The code should now resemble:

      [HttpPost]
      public bool UploadFile(string file)
      {
          var s3Client = new AmazonS3Client(accesskey, secretkey, RegionEndpoint.APSoutheast1);
    
          var fileTransferUtility = new TransferUtility(s3Client);
              if (file.Length > 0)
              {
                  var filePath = file;
                  var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                  {
                      BucketName = bucketName,
                      FilePath = filePath,
                      StorageClass = S3StorageClass.StandardInfrequentAccess,
                      PartSize = 6291456, // 6 MB.  
                      Key = keyName,
                  };
                  fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                  fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                  fileTransferUtility.Upload(fileTransferUtilityRequest);
                  fileTransferUtility.Dispose();
              }
          return true;
      }
    
  • Having the bucket policy set up in this way allows you to upload objects without triggering the "The bucket does not allow ACLs" error.

Solution 4: Setting Public Access Block and Ownership Controls

To resolve the "The bucket does not allow ACLs" error while uploading files to an S3 bucket using the AWS command line tool, you need to enable public access to the bucket. This can be done by executing the following commands:

  1. Set the Public Access Block configuration for the bucket:

    aws s3api put-public-access-block --bucket MY_BUCKET \
    --public-access-block-configuration 'BlockPublicAcls=false,
    IgnorePublicAcls=false,
    BlockPublicPolicy=false,
    RestrictPublicBuckets=false'
    

    This command sets the following settings for the bucket:

    • Block Public ACLs: Disabled, allowing you to set ACLs on objects in the bucket.
    • Ignore Public ACLs: Disabled, causing public ACLs to be honored.
    • Block Public Policy: Disabled, allowing you to set a public bucket policy.
    • Restrict Public Buckets: Disabled, allowing you to make the bucket public.
  2. Set the Object Ownership controls for the bucket:

    aws s3api put-bucket-ownership-controls --bucket MY_BUCKET \
    --ownership-controls 'Rules=[{ObjectOwnership="BucketOwnerPreferred"}]'
    

    This command sets the object ownership control for the bucket to "BucketOwnerPreferred". This means that objects uploaded to the bucket will be owned by the bucket owner, even if an ACL grants ownership to another entity.

By executing these commands, you will enable public access to your S3 bucket, allowing you to upload objects with ACLs. Remember to replace MY_BUCKET with the actual name of your bucket.