Quickly Using the SDK
Creating an AK and SK
- An AK is an access key ID. An AK maps to only one user but one user can have multiple AKs. The OBS system identifies users who access the system by their access key IDs.
- An SK is a secret access key, which is required to access OBS. You can generate authentication information based on the SKs and the request header fields. An SK matches an AK, and they group into a pair.
- Log in to OBS Console.
- In the upper right corner of the page, click the username and choose My Credentials.
- On the My Credentials page, select Access Keys in the navigation pane on the left.
- On the Access Keys page, click Create Access Key.
- In the Create Access Key dialog box that is displayed, enter the password and verification code.
- If you have not bound an email address or mobile number, you need to enter only the password.
- If you have bound an email address and a mobile number, you can select the verification either by email address or mobile number.
- Click OK.
- In the Download Access Key dialog box that is displayed, click OK to save the access keys to your browser's default download path.
- Open the downloaded credentials.csv file to obtain the access keys (AK and SK).
- A user can create a maximum of two valid access keys.
- To prevent the AK from being leaked, keep it secure. If you click Cancel in the dialog box, the access keys will not be downloaded, and cannot be obtained later. You can re-create an AK if you need to use it.
Obtaining Endpoints
- You can click here to view the endpoints and regions enabled for OBS.
The SDK allows you to pass endpoints with or without the protocol name. Suppose the endpoint you obtained is your-endpoint. The endpoint passed when initializing an instance of ObsClient can be http://your-endpoint, https://your-endpoint, or your-endpoint.
Initializing an Instance of ObsClient
# Import the module.
from obs import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
access_key_id='*** Provide your Access Key ***',
secret_access_key='*** Provide your Secret Key ***',
server='https://your-endpoint'
)
# Use the instance to access OBS.
# Close ObsClient.
obsClient.close()
Creating a Bucket
# Invoke createBucket to create a bucket.
resp = obsClient.createBucket('bucketname')
if resp.status < 300:
# Return the request ID.
print('requestId:', resp.requestId)
else:
# Return the error code.
print('errorCode:', resp.errorCode)
# Return error information.
print('errorMessage:', resp.errorMessage)
- Bucket names are globally unique. Ensure that the bucket you create is named differently from any other bucket.
- A bucket name must comply with the following rules:
- Contains 3 to 63 characters chosen from lowercase letters, digits, hyphens (-), and periods (.), and starts with a digit or letter.
- Cannot be an IP address or similar.
- Cannot start or end with a hyphen (-) or period (.).
- Cannot contain two consecutive periods (.), for example, my..bucket.
- Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.
- If you create buckets of the same name, no error will be reported and the bucket properties comply with those set in the first creation request.
- For more information, see Creating a Bucket.
- This parameter is not required if the endpoint belongs to the default region (cn-north-1). If the endpoint belongs to a region other than the default one, set this parameter to the region to which the endpoint belongs. Click here to query currently valid regions.
- When creating a bucket, you can specify its region. For details, see Creating a Bucket.
Uploading an Object
# Call the putContent interface to upload the object to the bucket.
resp = obsClient.putContent('bucketname', 'objectname', 'Hello OBS')
if resp.status < 300:
# Return the request ID.
print('requestId:', resp.requestId)
else:
# Return the error code.
print('errorCode:', resp.errorCode)
# Return error information.
print('errorMessage:', resp.errorMessage)
Last Article: Installing the SDK
Next Article: Initialization
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.