put_object Boto3 Docs 1.26.82 documentation

April 2024 · 10 minute read

Adds an object to a bucket. You must have WRITE permissions on a bucket to add an object to it.

Amazon S3 never adds partial objects; if you receive a success response, Amazon S3 added the entire object to the bucket.

Amazon S3 is a distributed system. If it receives multiple write requests for the same object simultaneously, it overwrites all but the last object written. Amazon S3 does not provide object locking; if you need this, make sure to build it into your application layer or use versioning instead.

To ensure that data is not corrupted traversing the network, use the Content-MD5 header. When you use this header, Amazon S3 checks the object against the provided MD5 value and, if they do not match, returns an error. Additionally, you can calculate the MD5 while putting an object to Amazon S3 and compare the returned ETag to the calculated MD5 value.

Note

Server-side Encryption

You can optionally request server-side encryption. With server-side encryption, Amazon S3 encrypts your data as it writes it to disks in its data centers and decrypts the data when you access it. You have the option to provide your own encryption key or use Amazon Web Services managed encryption keys (SSE-S3 or SSE-KMS). For more information, see Using Server-Side Encryption.

If you request server-side encryption using Amazon Web Services Key Management Service (SSE-KMS), you can enable an S3 Bucket Key at the object-level. For more information, see Amazon S3 Bucket Keys in the Amazon S3 User Guide .

Access Control List (ACL)-Specific Request Headers

You can use headers to grant ACL- based permissions. By default, all objects are private. Only the owner has full access control. When adding a new object, you can grant permissions to individual Amazon Web Services accounts or to predefined groups defined by Amazon S3. These permissions are then added to the ACL on the object. For more information, see Access Control List (ACL) Overview and Managing ACLs Using the REST API.

If the bucket that you're uploading objects to uses the bucket owner enforced setting for S3 Object Ownership, ACLs are disabled and no longer affect permissions. Buckets that use this setting only accept PUT requests that don't specify an ACL or PUT requests that specify bucket owner full control ACLs, such as the bucket-owner-full-control canned ACL or an equivalent form of this ACL expressed in the XML format. PUT requests that contain other ACLs (for example, custom grants to certain Amazon Web Services accounts) fail and return a 400 error with the error code AccessControlListNotSupported .

For more information, see Controlling ownership of objects and disabling ACLs in the Amazon S3 User Guide .

Note

If your bucket uses the bucket owner enforced setting for Object Ownership, all objects written to the bucket by any account will be owned by the bucket owner.

Storage Class Options

By default, Amazon S3 uses the STANDARD Storage Class to store newly created objects. The STANDARD storage class provides high durability and high availability. Depending on performance needs, you can specify a different Storage Class. Amazon S3 on Outposts only uses the OUTPOSTS Storage Class. For more information, see Storage Classes in the Amazon S3 User Guide .

Versioning

If you enable versioning for a bucket, Amazon S3 automatically generates a unique version ID for the object being stored. Amazon S3 returns this ID in the response. When you enable versioning for a bucket, if Amazon S3 receives multiple write requests for the same object simultaneously, it stores all of the objects.

For more information about versioning, see Adding Objects to Versioning Enabled Buckets. For information about returning the versioning state of a bucket, see GetBucketVersioning.

Related Resources

See also: AWS API Documentation

Request Syntax

object = bucket.put_object( ACL='private'|'public-read'|'public-read-write'|'authenticated-read'|'aws-exec-read'|'bucket-owner-read'|'bucket-owner-full-control', Body=b'bytes'|file, CacheControl='string', ContentDisposition='string', ContentEncoding='string', ContentLanguage='string', ContentLength=123, ContentMD5='string', ContentType='string', ChecksumAlgorithm='CRC32'|'CRC32C'|'SHA1'|'SHA256', ChecksumCRC32='string', ChecksumCRC32C='string', ChecksumSHA1='string', ChecksumSHA256='string', Expires=datetime(2015, 1, 1), GrantFullControl='string', GrantRead='string', GrantReadACP='string', GrantWriteACP='string', Key='string', Metadata={ 'string': 'string' }, ServerSideEncryption='AES256'|'aws:kms', StorageClass='STANDARD'|'REDUCED_REDUNDANCY'|'STANDARD_IA'|'ONEZONE_IA'|'INTELLIGENT_TIERING'|'GLACIER'|'DEEP_ARCHIVE'|'OUTPOSTS'|'GLACIER_IR', WebsiteRedirectLocation='string', SSECustomerAlgorithm='string', SSECustomerKey='string', SSEKMSKeyId='string', SSEKMSEncryptionContext='string', BucketKeyEnabled=True|False, RequestPayer='requester', Tagging='string', ObjectLockMode='GOVERNANCE'|'COMPLIANCE', ObjectLockRetainUntilDate=datetime(2015, 1, 1), ObjectLockLegalHoldStatus='ON'|'OFF', ExpectedBucketOwner='string' ) 
ParametersReturn type

s3.Object

Returns

Object resource

ncG1vNJzZmian6m8dHrAppizp56WxLR6wqikaK5hZLGwr9SmnKeskam2sLqOmqeiZ2Fjf3d6l2tmq52Wmr%2BmusKeZqydoqu2pLHSaKpsZ5KqsKyx02inrqyPpK%2BrscKtZaGsnaE%3D