Uploading Objects - File-Based
API Description
You can use this API to upload a file or folder to a specified bucket.
There is no folder in its real meaning in OBS, because all elements in buckets are objects. To create a folder in OBS is essentially to create an object whose size is 0 and whose name ends with a slash (/). Such objects have no difference from other objects and can be downloaded and deleted, except that they are displayed as folders in OBS Console.
Method Definition
ObsClient.putFile(bucketName, objectKey, file_path, metadata, headers, progressCallback)
Request Parameters
|
Field |
Type |
Optional or Mandatory |
Description |
|---|---|---|---|
|
bucketName |
str |
Mandatory |
Bucket name |
|
objectKey |
str |
Mandatory |
Object name or the name of the uploaded file |
|
file_path |
str |
Mandatory |
Full path of the file or folder to be uploaded, for example, aa/bb.txt or aa/. |
|
metadata |
dict |
Optional |
Customized metadata of the object |
|
headers |
Optional |
Additional header of the request for uploading an object |
|
|
progressCallback |
callable |
Optional |
Callback function for obtaining the upload progress
NOTE:
This function contains the following parameters in sequence: number of uploaded bytes, total number of bytes, and used time (unit: second). For details about the sample code, see Uploading an Object – Obtaining the Upload Progress. The upload progress can be obtained only during the upload process. |
If file_path is a folder, contentLength, md5, and contentType in headers cannot take effect.
Returned Results
|
Type |
Description |
|---|---|
|
SDK common result object |
|
GetResult.body Type |
Description |
|---|---|
|
Response result of the request for uploading an object |
If file_path is a folder, the returned result is a list of GetResult.
Sample Code
try:
from obs import PutObjectHeader
headers = PutObjectHeader()
headers.contentType = 'text/plain'
resp = obsClient.putFile('bucketname', 'objectkey', 'localfile', metadata={'meta1':'value1', 'meta2':'value2'}, headers=headers)
if resp.status < 300:
print('requestId:', resp.requestId)
print('etag:', resp.body.etag)
print('versionId:', resp.body.versionId)
print('storageClass:', resp.body.storageClass)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
except:
import traceback
print(traceback.format_exc())
Last Article: Uploading Objects - Streaming
Next Article: Uploading Objects - Appending
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.