S3 giving me NoSuchKey error even when the key exists

前端 未结 5 1028
野的像风
野的像风 2021-02-13 01:15

This is my boto3 command for getting the object with a specific key from an S3 bucket:

resp = s3client.get_object(Bucket=\'<>-<>\', Key=\'MzA1MjY1Nzkz         


        
5条回答
  •  长发绾君心
    2021-02-13 01:58

    A generic answer that may useful to those who are thinking about file paths and may be new to the AWS S3 terminology. Not getting the "name" and "key" right will often lead to an exception with the message An error occurred (NoSuchKey)... as posted in this question.

    Let's say that you have a JPEG file stored in some "path" in a bucket. Navigating to this object on the AWS console shows the S3 URI to be:

    s3://my-bucket/some/very/long/path/my-image.jpeg

    You could read the my-image.jpeg object in Python with this basic example:

    import boto3
    
    s3client = boto3.client('s3', region_name='us-east-1')
    
    bucket_name = 'my-bucket'
    object_key = 'some/very/long/path/my-image.jpeg'
    
    try:
        s3obj = s3client.get_object(Bucket=bucket_name, Key=object_key)
    except Exception as e:
        print(f"Error reading key {object_key} from bucket {bucket_name}: {e}")
    else:
       print(f"Got object: {s3obj}")
    

提交回复
热议问题