Uploading a Part
Function
This API is used to upload a part to a specified bucket after a multipart upload is initiated. In the upload request, the multipart upload ID must be included. Except for the part lastly being uploaded whose size ranges from 0 to 5 GB, sizes of the other parts range from 100 KB to 5 GB. Part numbers can be any number from 1 to 10,000.
When uploading a part, you must specify its upload ID and part number. The part number ranges from 1 to 10,000. A part number uniquely identifies a part and its position in the object you are uploading. If you upload a new part with the same part number as that of a previous part, the previously uploaded part will be overwritten. Whenever you upload a part, OBS returns the ETag header in the response. For each part upload task, you must record the part number and ETag value. These values are required in subsequent requests for you to complete a multipart upload.
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
Restrictions
- To upload a part, you must be the bucket owner or have the required permission (obs:object:PutObject granted using IAM or PutObject granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
- After initiating a multipart upload and uploading one or more parts, you must complete or abort the multipart upload. Otherwise, fragments will occupy your storage space and incur storage fees.
- partNumber in a multipart upload must be unique. When the same partNumber of an object is used for concurrent uploads, the server follows the Last Write Wins policy, where the Last Write is determined by the time the part's metadata was created. To ensure data accuracy, the client must ensure that the same part of an object cannot be uploaded concurrently.
Method
ObsClient.uploadPart(params)
Request Parameters
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| Bucket | String | Yes | Explanation: Bucket name Restrictions:
Default value: None |
| Key | String | Yes | Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None NOTE: The object URL is in the following format: https://Bucket name.Domain name/Folder directory level/Object name. If this object is stored in the root directory of the bucket, its URL does not contain the folder directory level. |
| RequestDate | String or Date | No | Explanation: Request time NOTE: When the parameter type is String, the value must comply with the ISO8601 or RFC822 standard. |
| PartNumber | Number | Yes | Explanation: Part number Value range: 1 to 10000 Default value: None |
| UploadId | String | Yes | Explanation: Multipart upload ID, which can be obtained by initiating a multipart upload, for example, 000001648453845DBB78F2340DD460D8 Restrictions: The value must contain 32 characters. Default value: None |
| ContentMD5 | String | No | Explanation: Base64-encoded MD5 value of the part to be uploaded, which uniquely identifies the part content and can be used to identify whether the object content is changed. Value range: The value must contain 32 characters. Default value: None |
| Body | String | No | Explanation: Content of the object to be uploaded Restrictions: This parameter cannot be used together with SourceFile. Default value: None |
| SourceFile | File or Blob | No | Explanation: Source file of the part to be uploaded (The browser must support FileReader.) Restrictions: This parameter cannot be used together with Body. Default value: None |
| ProgressCallback | Function | No | Explanation: Callback function for obtaining the upload progress Default value: None NOTE: This callback function contains the following parameters in sequence: number of uploaded bytes, total bytes, and used time (in seconds). |
| Offset | Number | No | Explanation: Start offset of a part in the source file Restrictions: This parameter is valid only when SourceFile is set. Value range: A non-negative integer not exceeding the size of the object to be uploaded, in bytes Default value: 0 |
| PartSize | Number | No | Explanation: Size of a part in the source file Restrictions:
Value range: The value ranges from 100 KB to 5 GB, in bytes. Default value: The default value is the file size minus Offset, in bytes. |
| SseKms | String | No | Explanation: Encryption method Value range: kms: SSE-KMS is used for encryption. AES256: SSE-OBS and the AES256 algorithm are used for encryption. Default value: None |
| SseKmsKey | String | No | Explanation: ID of the KMS master key used in SSE-KMS mode Value range: Valid value formats are as follows:
In the preceding formats:
Default value:
|
| SseKmsProjectId | String | No | Explanation: ID of the project (not an enterprise project) where the KMS CMK belongs when SSE-KMS is used Value range: Project ID that matches the key specified in SseKmsKey Default value: None |
| SseC | String | No | Explanation: Algorithm used for SSE-C Value range: AES256: SSE-C and the AES-256 algorithm are used. Default value: None |
| SseCKey | String | No | Explanation: Custom key used in SSE-C encryption. The value is a Base64-encoded 256-bit key. Default value: None |
Responses
| Parameter | Type | Description |
| Status | number | Explanation: HTTP status code returned by the OBS server Value range: A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes. |
| Code | string | Explanation: Error code returned by the OBS server |
| Message | string | Explanation: Error description returned by the OBS server |
| HostId | string | Explanation: Request server ID returned by the OBS server |
| RequestId | string | Explanation: Request ID returned by the OBS server |
| Id2 | string | Explanation: Request ID2 returned by the OBS server |
| Indicator | string | Explanation: Error code details returned by the OBS server |
| Parameter | Type | Description |
|---|---|---|
| ETag | String | Explanation: Base64-encoded 128-bit MD5 digest of a part. ETag is the unique identifier of the part content. It can be used to determine whether the part content is changed. Value range: The value must contain 32 characters. Default value: None |
Sample Code
// Create an ObsClient instance.
var obsClient = new ObsClient({
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
access_key_id: process.env.AccessKeyID,
secret_access_key: process.env.SecretAccessKey,
// CN-Hong Kong is used here in this example. Replace it with the one currently in use.
server: 'https://obs.ap-southeast-1.myhuaweicloud.com'
});
const bucketname = 'examplebucket';
const objectname = 'exampleobject';
const PartSize = 5 * 1024 * 1024;
const Uploadid = 'upload id from initiateMultipartUpload';
const file = document.getElementById('input-file').files[0];
const lastPartSize = file.size % PartSize;
// Number of parts
const count = Math.ceil(file.size / PartSize);
// Upload part n.
const uploadPart = (n) => {
obsClient.uploadPart({
Bucket: bucketname,
Key: objectname,
// Set the part number, which ranges from 1 to 10000.
PartNumber: n,
// Set the upload ID.
UploadId,
// Specify the large file to be uploaded.
SourceFile: file,
// Set the part size.
PartSize: count === n ? lastPartSize : PartSize,
// Set the start offset.
Offset: (n-1) * PartSize
}, function (err, result) {
if(err){
console.log('Error-->' + err);
}else{
console.log('Status-->' + result.CommonMsg.Status);
if(result.CommonMsg.Status < 300 && result.InterfaceResult){
console.log('ETag-->' + result.InterfaceResult.ETag);
}
}
});
}
// Upload part 1.
uploadPart(1);
If the ETag value obtained is undefined, you need to configure a CORS rule and add the ETag to the additional header. For details, see ETag.
- Use the PartNumber parameter to specify the part number, the UploadId parameter to specify the globally unique ID, the SourceFile parameter to specify the to-be-uploaded file, the PartSize parameter to set the part size, and the Offset parameter to set the start offset of the file.
- SourceFile must indicate a File or Blob object. For example, on an HTML page, use an input tag whose type is file to specify the to-be-uploaded file: <input type="file" id="input-file"/>.
- Except the part last uploaded, each of the other parts must be larger than 100 KB. The size of each part is not verified during its upload, because the system cannot define whether it is the last part. The part size is verified when the parts are assembled.
- OBS will return ETags (MD5 values) of the received parts to users.
- You can use the ContentMD5 parameter to set the MD5 value of the uploaded data.
- Part numbers range from 1 to 10,000. If a part number exceeds this range, OBS will return error 400 Bad Request.
- The minimum part size supported by an OBS 3.0 bucket is 100 KB, and that supported by an OBS 2.0 bucket is 5 MB. You are advised to perform multipart uploads on OBS 3.0 buckets.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot