Quickly Using the SDK
Creating an AK and SK
OBS employs access keys (AK and SK) in user accounts for signature verification to ensure that only authorized accounts can access specified OBS resources. The following explains the 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.
The procedure for creating access keys is as follows:
- 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, enter only the password.
- If you have bound an email address and a mobile number, you can select the verification by either email or mobile phone.
- 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
Each time you want to send an HTTP/HTTPS request to OBS, you must create an ObsClient struct. Sample code is as follows:
// Import the dependency package. import ( "obs" ) func main() { var ak = "*** Provide your Access Key ***" var sk = "*** Provide your Secret Key ***" var endpoint = "https://your-endpoint" // Create an ObsClient struct. var obsClient, err = obs.New(ak, sk, endpoint) if err == nil { // Use the struct to access OBS. // Close obsClient. obsClient.Close() } }
- For more information, see chapter "Initialization."
- If the value of endpoint does not contain any protocol, HTTPS is used by default.
- For the sake of high DNS resolution performance and OBS reliability, you can set endpoint only to the domain name of OBS, instead of the IP address.
Creating a Bucket
A bucket is a global namespace of OBS and is a data container. It functions as a root directory of a file system and can store objects. Sample code:
input := &obs.CreateBucketInput{}
input.Bucket = "bucketname"
output, err := obsClient.CreateBucket(input)
if err == nil {
fmt.Printf("RequestId:%s\n", output.RequestId)
} else {
if obsError, ok := err.(obs.ObsError); ok {
fmt.Println(obsError.Code)
fmt.Println(obsError.Message)
} else {
fmt.Println(err)
}
}
- 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.
When creating a bucket, you do not need to specify the region if the endpoint belongs to the default region (cn-north-1). If the endpoint belongs to a region other than the default one, you need to specify the region to which the endpoint belongs. For more information, refer to Creating a Bucket. Click here to query currently valid regions.
Uploading an Object
Sample code:
input := &obs.PutFileInput{}
input.Bucket = "bucketname"
input.Key = "objectname"
input.SourceFile = "localfile" // localfile is the path of the local file to be uploaded. The file name must be specified.
output, err := obsClient.PutFile(input)
if err == nil {
fmt.Printf("RequestId:%s\n", output.RequestId)
} else {
if obsError, ok := err.(obs.ObsError); ok {
fmt.Println(obsError.Code)
fmt.Println(obsError.Message)
} else {
fmt.Println(err)
}
} 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.