Search code examples
amazon-web-servicesamazon-s3aws-cli

AWS CP but skip access denied


In a bucket where you have access to some files only, is it possible to use a single cp command but skip all the files you do not have access to?

Ex if I run aws s3 cp s3://mybucket/ .

I will get an error saying access denied to some file, I'd like to skip these errors and continue downloading what I have access to.


Solution

  • To test your scenario, I did the following:

    • Created a folder in an S3 bucket
    • Added a Bucket Policy that denied access to one of the objects
    • Performed a copy with:
    aws s3 cp --recursive s3://my-bucket/foo/ .
    

    The result was:

    download failed: s3://my-bucket/foo/f1 to ./f1 An error occurred (AccessDenied) when calling the GetObject operation: Access Denied
    download: s3://my-bucket/foo/f2 to ./f2                            
    

    It successfully 'skipped-over' the object that it couldn't access. Therefore, the default behaviour should suit your needs.

    I also tried it with:

    aws s3 sync  s3://my-bucket/foo/ .
    

    It gave an error for the inaccessible file, but kept going. So this is also a viable option for you.

    Summary: The AWS CLI cannot detect access permissions prior to attempting to copy an object, but it gracefully handles errors on individual objects.