• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Object Upload
  5. Setting Object Properties

Setting Object Properties

You can set properties for an object when uploading it. Object properties include the object length, MIME type, MD5 value (for verification), storage class, and customized metadata. You can set properties for an object that is being uploaded in text-based, streaming, file-based, or multipart mode or when copying the object.

The following table describes object properties.

Property Name

Description

Default Value

Content-Length

Indicates the object length. If the object length exceeds the flow or file length, the object will be truncated.

Actual length of the stream or file

Content-Type

Indicates the MIME type of the object, which defines the type and network code of the object as well as in which mode and coding will the browser read the object.

binary/octet-stream

Content-MD5

Indicates the base64-encoded digest of the object data. It is provided for the OBS server to verify data integrity.

None

Storage class

Indicates the storage class of the object. Different storage classes meet different needs for storage performance and costs. The value defaults to be the same as the object's residing bucket and can be changed.

None

Customized metadata

Indicates the user-defined description of the object. It is used to facilitate the customized management on the object.

None

Setting the Length for an Object

You can call PutObjectHeader.contentLength to set the object length. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.put_object_header import PutObjectHeader
headers = PutObjectHeader()
# Only 100 bytes of the object will be uploaded.
headers.contentLength = 100
content = open('localfile', 'rb')
resp = obsClient.putContent('bucketname', 'objectkey', content=content, headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Setting the MIME Type for an Object

You can call PutObjectHeader.contentType to set the MIME type for an object. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.put_object_header import PutObjectHeader
headers = PutObjectHeader()
# Upload an image.
headers.contentType = 'image/jpeg'
resp = obsClient.putFile('bucketname', 'objectkey.jpg', file_path='localimage.jpg', headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:

If this property is not specified, SDK will automatically identify the MIME type according to the suffix of the uploaded object. For example, if the suffix of the object is .xml (.html), the object will be identified as an application/xml (text/html) file.

Setting the MD5 Value for an Object

You can call PutObjectHeader.md5 to set the MD5 value for an object. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.put_object_header import PutObjectHeader
headers = PutObjectHeader()
# Set the MD5 value.
headers.md5 = 'your md5 which should be encoded by base64'
resp = obsClient.putFile('bucketname', 'objectkey.jpg', file_path='localimage.jpg', headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:
  • The MD5 value of an object must be a base64-encoded digest.
  • The OBS server will compare this MD5 value with the MD5 value obtained by object data calculation. If the two values are not the same, the upload fails and an HTTP 400 error will be returned.
  • If the MD5 value is not specified, the OBS server will skip MD5 value verification.

Setting the Storage Class for an Object

You can call PutObjectHeader.storageClass to set the storage class for an object. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.put_object_header import PutObjectHeader
headers = PutObjectHeader()
# Set the storage class to OBS Cold.
headers.storageClass = 'GLACIER'
resp = obsClient.putFile('bucketname', 'objectkey', file_path='localfile', headers=headers)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:
  • If you do not set the storage class for an object, the storage class of the object will be the same as that of its residing bucket.
  • OBS provides objects with three storage classes which are consistent with those provided for buckets.
  • Before downloading a Cold object, you must restore it first.

Customizing Metadata for an Object

You can use the metadata parameter to customize the metadata for the object. Sample code is as follows:

# Import the module.
from com.obs.client.obs_client import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)
metadata = {'property1' : 'property-value1', 'property2' : 'property-value2'}
resp = obsClient.putFile('bucketname', 'objectkey', file_path='localfile', metadata=metadata)

if resp.status < 300:
    print('requestId:', resp.requestId)
else:    
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:
  • In the preceding code, two pieces of metadata named property1 and property2 are customized and their respective values are set to property-value1 and property-value2.
  • An object can have multiple pieces of metadata. The total metadata size cannot exceed 8 KB.
  • The customized object metadata can be obtained by using ObsClient.getObjectMetadata. For details, see Obtaining Object Metadata.
  • When you call ObsClient.getObject to download an object, its customized metadata will also be downloaded.