Use the PutObject API to upload a file to an Object Storage Service (OSS) bucket. The maximum size of a file that can be uploaded in a single operation is 5 GB.
Request syntax
PUT /ObjectName HTTP/1.1
Content-Length: ContentLength
Content-Type: ContentType
Host: BucketName.oss-cn-hangzhou.aliyuncs.com
Date: GMT Date
Authorization: SignatureValueUsage notes
The maximum size of a file that can be uploaded in a single operation is 5 GB. To upload a file larger than 5 GB, use the multipart upload feature.
When you upload a file with the same name as an existing file, the existing file is overwritten by default and a 200 OK status code is returned. You can set a parameter to prevent overwrites to avoid accidentally overwriting important files.
OSS uses a flat storage structure and does not have directories like a traditional file system. You can simulate a folder structure by creating an empty object that ends with a forward slash (/).
Permissions
Versioning
Request parameters
OSS supports standard HTTP request headers such as Cache-Control, Expires, Content-Encoding, Content-Disposition, and Content-Type. If you set these request headers, their values are automatically applied when the file is downloaded.
Parameter | Type | Required | Example | Description |
Authorization | String | No | OSS qn6q**************:77Dv**************** | Indicates that the request has been authenticated and authorized. For more information about how to calculate the Authorization value, see Include a signature in the header. The Authorization header is typically required. However, you do not need to include this header if you include the signature in the URL. For more information, see Include a signature in the URL. Default value: none |
Cache-Control | String | No | no-cache | Specifies the caching behavior when an object is downloaded. Valid values:
Default value: none |
Content-Disposition | String | No | attachment | Specifies how the object is displayed. Valid values:
If you want to download the object to the specified download path of the browser, take note of the following items: Note
Whether an object is previewed or downloaded as an attachment when the object is accessed by using the object URL is determined by the creation time of the bucket in which the object is stored, the activation time of OSS, and the domain name type. For more information, see What do I do if an image object is downloaded as an attachment but cannot be previewed when I access the image object by using its URL? Default value: none |
Content-Encoding | String | No | identity | Declares the codec of the object. You must specify the actual codec of the object. Otherwise, parsing or download failures may occur on the client. If the object is not encoded, leave this header empty. Valid values:
Default value: none |
Content-MD5 | String | No | eB5eJF1ptWaXm4bijSPyxw== | Used to check the integrity of the message content. Content-MD5 is a value generated by the MD5 algorithm. If you set this header, OSS calculates the Content-MD5 hash of the message body and checks for consistency. For more information, see How to calculate Content-MD5. To ensure data integrity, OSS provides multiple methods to verify the MD5 hash of data. To perform MD5 verification using Content-MD5, add the Content-MD5 header to the request. Default value: none |
Content-Length | String | No | 344606 | The size of the HTTP message body to be transferred, in bytes. If the value of the Content-Length header is smaller than the actual size of the data transferred in the request body, OSS still creates the object. However, the object size will be equal to the size defined in Content-Length, and the excess data is discarded. |
Expires | String | No | Wed, 08 Jul 2015 16:57:01 GMT | Specifies the expiration time of the object. For more information, see RFC2616. Default value: none |
x-oss-forbid-overwrite | String | No | false | Specifies whether to overwrite an object that has the same name during a PutObject operation. If the destination bucket has versioning enabled or suspended, the x-oss-forbid-overwrite request header is invalid. This means that an object with the same name can be overwritten.
Setting the x-oss-forbid-overwrite request header affects QPS performance. If many operations (QPS>1000) require the x-oss-forbid-overwrite request header, contact technical support to prevent impacts on your business operations. Default value: false |
x-oss-server-side-encryption | String | No | AES256 | Specifies the server-side encryption method when you create an object. Valid values: AES256, KMS, If you specify this header, it is returned in the response header. OSS encrypts and stores the uploaded object. When you download the object, the response header includes x-oss-server-side-encryption, and its value is set to the encryption algorithm of the object. |
x-oss-server-side-encryption-key-id | String | No | 9468da86-3509-4f8d-a61e-6eab1eac**** | The ID of the customer master key (CMK) that is managed by KMS. This header is valid only when x-oss-server-side-encryption is set to KMS. |
x-oss-object-acl | String | No | default | Specifies the access permissions of the object when it is created in OSS. Valid values:
For more information about access permissions, see Object ACL. |
x-oss-storage-class | String | No | Standard | Specifies the storage class of the object. For a bucket of any storage class, if you specify this parameter when you upload an object, the object is stored in the specified class. For example, if you set x-oss-storage-class to Standard when you upload an object to an Infrequent Access (IA) bucket, the object is stored as a Standard object. Valid values:
For more information, see Storage classes. |
x-oss-meta-* | String | No | x-oss-meta-location | When you use the PutObject API, parameters prefixed with x-oss-meta- are considered user-defined metadata, such as Metadata supports hyphens (-), numbers, and lowercase letters (a-z). Uppercase letters are converted to lowercase. Other characters, including underscores (_), are not supported. |
x-oss-tagging | String | No | TagA=A&TagB=B | Specifies tags for the object in a key-value format. You can set multiple tags at the same time, such as Note The key and value must be URL-encoded. The key is required, but the value is optional. For example, you can set the object tags to |
For more information, see Common Response Headers.
Response parameters
Parameter | Type | Example | Description |
Content-MD5 | String | 1B2M2Y8AsgTpgAmY7PhC**** | The MD5 hash of the uploaded file. Important The MD5 hash is the hash of the file obtained after the client completes the upload, not the MD5 hash of the response body. |
x-oss-hash-crc64ecma | String | 316181249502703**** | The CRC-64 value of the uploaded file. |
x-oss-version-id | String | CAEQNhiBgMDJgZCA0BYiIDc4MGZjZGI2OTBjOTRmNTE5NmU5NmFhZjhjYmY0**** | The version ID of the file. This response header is returned only when the file is uploaded to a bucket with versioning enabled. |
For more information, see Common Response Headers.
Examples
Simple upload
Request example
PUT /test.txt HTTP/1.1 Host: test.oss-cn-zhangjiakou.aliyuncs.com User-Agent: aliyun-sdk-python/2.6.0(Windows/7/AMD64;3.7.0) Accept: */* Connection: keep-alive Content-Type: text/plain Date: Tue, 04 Dec 2018 15:56:37 GMT Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e Transfer-Encoding: chunkedResponse example
HTTP/1.1 200 OK Server: AliyunOSS Date: Tue, 04 Dec 2018 15:56:38 GMT Content-Length: 0 Connection: keep-alive x-oss-request-id: 5C06A3B67B8B5A3DA422299D ETag: "D41D8CD98F00B204E9800998ECF8****" x-oss-hash-crc64ecma: 316181249502703**** Content-MD5: 1B2M2Y8AsgTpgAmY7PhC**** x-oss-server-time: 7
Set the storage class
Request example
PUT /oss.jpg HTTP/1.1 Host: oss-example.oss-cn-hangzhou.aliyuncs.com Cache-control: no-cache Expires: Fri, 28 Feb 2012 05:38:42 GMT Content-Disposition: attachment;filename=oss_download.jpg Date: Fri, 24 Feb 2012 06:03:28 GMT Content-Type: image/jpg Content-Length: 344606 x-oss-storage-class: Archive Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,AdditionalHeaders=content-disposition;content-length,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e [344606 bytes of object data]Response example
HTTP/1.1 200 OK Server: AliyunOSS Date: Sat, 21 Nov 2015 18:52:34 GMT Content-Type: image/jpg Content-Length: 0 Connection: keep-alive x-oss-request-id: 5650BD72207FB30443962F9A ETag: "A797938C31D59EDD08D86188F6D5B872"
Enable versioning
Request example
PUT /test HTTP/1.1 Content-Length: 362149 Content-Type: text/html Host: versioning-put.oss-cn-hangzhou.aliyuncs.com Date: Tue, 09 Apr 2019 02:53:24 GMT Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,AdditionalHeaders=content-length,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218eResponse example
HTTP/1.1 200 OK Server: AliyunOSS Date: Tue, 09 Apr 2019 02:53:24 GMT Content-Length: 0 Connection: keep-alive x-oss-request-id: 5CAC0A3DB7AEADE01700**** x-oss-version-id: CAEQNhiBgMDJgZCA0BYiIDc4MGZjZGI2OTBjOTRmNTE5NmU5NmFhZjhjYmY0**** ETag: "4F345B1F066DB1444775AA97D5D2****"
Error codes
Error code | HTTP status code | Description |
MissingContentLength | 411 | The request header does not use chunked encoding or the Content-Length parameter is not set. |
InvalidEncryptionAlgorithmError | 400 | The value specified for x-oss-server-side-encryption is invalid. Valid values: AES256, KMS, . |
AccessDenied | 403 | The user does not have the required access permissions for the specified bucket when adding the object. |
NoSuchBucket | 404 | The specified bucket does not exist when adding the object. |
InvalidObjectName | 400 | The object name is invalid. This can be because the object name is not specified, exceeds the length limit, or is invalid. |
InvalidArgument | 400 | This error can be returned for the following reasons:
|
RequestTimeout | 400 | Content-Length is specified, but no message body is sent, or the sent message body is smaller than the specified size. In this case, the server waits until the request times out. |
Bad Request | 400 | If you specify Content-MD5 in the request, OSS calculates the MD5 hash of the sent data and compares it with the value of Content-MD5 in the request. If the two values do not match, this error is returned. |
KmsServiceNotEnabled | 403 | You specified KMS for x-oss-server-side-encryption but have not purchased a KMS suite in advance. |
FileAlreadyExists | 409 | Possible causes:
|
FileImmutable | 409 | This error is returned if you attempt to delete or modify data in a bucket that is in a protected state. |
Integration methods
FAQ
How do I modify the metadata of an uploaded file?
You can modify file metadata using the OSS console, ossbrowser, SDKs for different languages, the ossutil command line interface, or the REST API. For example, you can change the Content-Type from application/octet-stream to image/jpeg. For more information, see Manage object metadata.
Why is the Expires header I set not working?
Cache header priority
If you set both
ExpiresandCache-Control,Cache-Controlhas a higher priority. IfCache-Controlincludes a caching directive, such asmax-age=3600, theExpiresheader may be ignored.ExpiresIncorrect settingThe value of the Expires header must be a future time in GMT format. The following code provides an example of how to set this header using the Node.js SDK:
const OSS = require('ali-oss'); // Create an OSS client instance. const client = new OSS({ // Replace yourregion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to oss-cn-hangzhou. region: 'yourregion', // Obtain access credentials from environment variables. Before running this example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, // Specify the bucket name. bucket: 'examplebucket', }); async function setExpires(objectName, expiresDate) { try { const result = await client.copy(objectName, objectName, { meta: { 'Expires': expiresDate.toGMTString() } }); console.log('Expires header set successfully.'); } catch (error) { console.error('Error setting Expires header:', error); } } // Set the absolute expiration time for the cached content. const expiresDate = new Date('2024-10-12T00:00:00.000Z'); setExpires('your-object-name', expiresDate);