Example

Last Updated: Aug 08, 2017

Install and configure osscmd

After you download SDK installer in Linux or Windows, unzip the downloaded packet to start using osscmd.

You can directly invoke python osscmd to get instructions for use. Every command has two modes for execution. Take querying the user-created bucket for example. The gs command (short for “get service”) will be executed.

  • Method 1: No ID or Key is specified, and osscmd will read the ID and Key from default files.
  1. $ python osscmd gs
  2. can't get accessid/accesskey, setup use : config --id=accessid --key=accesskey

Notice: In the case of such prompts, it indicates that the ID and Key are not properly configured. See the configuration command in Step 2.

Once the ID and Key are properly configured and valid, run the command

  1. $ python osscmd gs
  2. 2013-07-19 08:11 test-oss-sample
  3. Bucket Number is: 1
  • Method 2: Specify the ID and Key in the command and osscmd will read ID and Key from the command line. If the ID and Key are valid, run the command and the following result will show.
  1. $ python osscmd gs --id=your_id --key=your_key
  2. 2013-07-19 08:11 test-oss-sample
  3. Bucket Number is: 1

To configure users’ ID and Key to the default files, run the following commands. The default oss host is oss.aliyuncs.com.

  1. $python osscmd config --id=YOUR_ID --key=YOUR_KEY

If you see a prompt saying “Your configuration is saved into” or similar, it indicates the ID and Key have been saved successfully.

Basic operations

List Created Bucket

  1. $python osscmd getallbucket

The output will be empty if the OSS user didn’t create any buckets.

Create Bucket

Create a bucket named mybucketname.

  1. $python osscmd createbucket mybucketname

Creating a bucket named “mybucketname” may fail because the name of the bucket in OSS is globally unique and someone might have created this bucket. In this case, you need to change the name. For example, you can add a specific date to the bucket name.

Check whether the bucket has been created successfully

  1. $python osscmd getallbucket

If it fails, check the error message returned.

View Object

After a bucket is successfully created, check the objects in the bucket.

  1. $python osscmd list oss://mybucketname/

There is no objects in the bucket, so the output is empty.

Upload Object

Upload an object to the bucket. If the local file is named local_existed_file, its MD5 value is shown as below.

  1. $ md5sum local_existed_file 7625e1adc3a4b129763d580ca0a78e44 local_existed_file
  2. $ python osscmd put local_existed_file oss://mybucketname/test_object

Notice: The md5sum command is used on Linux instead of Windows.

View Object Again

If it is successfully created, check the object again in bucket.

  1. $python osscmd list oss://mybucketname/

Download Object

Download an object from the bucket to local and compare the md5 value of the file downloaded.

  1. $ python osscmd get oss://mybucketname/test_object download_file
  2. $ md5sum download_file
  3. 7625e1adc3a4b129763d580ca0a78e44 download_file

Notice: The md5sum command is used on Linux instead of Windows.

Delete Object

  1. $ python osscmd delete oss://mybucketname/test_object

Delete Bucket

Notice: If there are objects in the bucket, the bucket cannot be deleted.

  1. $ python osscmd deletebucket mybucketname

Usage Lifecycle

Configure an xml text file for lifecycle

  1. <LifecycleConfiguration>
  2. <Rule>
  3. <ID>1125</ID>
  4. <Prefix>log_backup/</Prefix>
  5. <Status>Enabled</Status>
  6. <Expiration>
  7. <Days>2</Days>
  8. </Expiration>
  9. </Rule>
  10. </LifecycleConfiguration>

This indicates deleting the objects of more than two days old to the current time and with the prefix of log_backup/ in the bucket. For detailed rule configuration, refer to API Reference.

Write Lifecycle

  1. python osscmd putlifecycle oss://mybucket lifecycle.xml
  2. 0.150(s) elapsed

Read Lifecycle

  1. python osscmd getlifecycle oss://mybucket
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <LifecycleConfiguration>
  4. <Rule>
  5. <ID>1125</ID>
  6. <Prefix>log_backup/</Prefix>
  7. <Status>Enabled</Status>
  8. <Expiration>
  9. <Days>2</Days>
  10. </Expiration>
  11. </Rule>
  12. </LifecycleConfiguration>
  13. 0.027(s) elapsed

Delete Lifecycle

  1. python osscmd deletelifecycle oss://mybucket
  2. 0.139(s) elapsed

Read Lifecycle

  1. python osscmd getlifecycle oss://mybucket
  2. Error Headers:
  3. [('content-length', '288'), ('server', 'AliyunOSS'), ('connection', 'close'), ('x-oss-request-id', '54C74FEE5D7F6B24E5042630'), ('date', 'Tue, 27 Jan 2015 08:44:30 GMT'), ('content-type', 'application/xml')]
  4. Error Body:
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <Error>
  7. <BucketName>mybucket</BucketName>
  8. <Code>NoSuchLifecycle</Code>
  9. <Message>No Row found in Lifecycle Table.</Message>
  10. <RequestId>54C74FEE5D7F6B24E5042630</RequestId>
  11. <HostId>mybucket.oss-maque-hz-a.alibaba.net</HostId>
  12. </Error>
  13. Error Status:
  14. 404
  15. getlifecycle Failed!

Anti-leech Settings

Allow access of blank referer

  1. $osscmd putreferer oss://test --allow_empty_referer=true
  2. 0.004(s) elapsed

Get Configured Referer

  1. $osscmd getreferer oss://test
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <RefererConfiguration>
  4. <AllowEmptyReferer>true</AllowEmptyReferer>
  5. <RefererList />
  6. </RefererConfiguration>

Do not allow blank referer. Only allow test referer requests

  1. $osscmd putreferer oss://test --allow_empty_referer=false --referer='www.test.com'
  2. 0.092(s) elapsed

Get Configured Referer

  1. $osscmd getreferer oss://test
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <RefererConfiguration>
  4. <AllowEmptyReferer>false</AllowEmptyReferer>
  5. <RefererList>
  6. <Referer>www.test.com</Referer>
  7. </RefererList>
  8. </RefererConfiguration>

Do not allow blank referer. Only allow test and test1 referer requests

  1. $osscmd putreferer oss://test --allow_empty_referer=false --referer='www.test.com,www.test1.com'

Get Configured Referer

  1. $osscmd getreferer oss://test
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <RefererConfiguration>
  4. <AllowEmptyReferer>false</AllowEmptyReferer>
  5. <RefererList>
  6. <Referer>www.test.com</Referer>
  7. <Referer>www.test1.com</Referer>
  8. </RefererList>
  9. </RefererConfiguration>

Use logging

Set logging

  1. $osscmd putlogging oss://mybucket oss://myloggingbucket/mb

Get logging

  1. $osscmd getlogging oss://mybucket
Thank you! We've received your feedback.