安裝配置osscmd
在Linux或者Windows上下載SDK安裝包後,解壓縮後就可以使用 osscmd了。
使用時直接調用python osscmd即可獲取相應的說明。每種命令有兩種執行模式。以查詢用戶所建立的bucket為例子。執行的是gs命令,get service的簡寫。
- 方法1:不指定ID和KEY,osscmd從預設檔案中讀取ID和KEY。
$ python osscmd gs can't get accessid/accesskey, setup use : config --id=accessid --key=accesskey
说明 如果出現這樣的提示表明沒有配置好ID和KEY,見步驟2中提示的配置命令。 如果配置好ID和KEY,並且ID和KEY有效,執行$ python osscmd gs 2013-07-19 08:11 test-oss-sample Bucket Number is: 1
- 方法2:直接在命令中指定ID和KEY,osscmd從命令列中讀取ID和KEY。如果ID和KEY有效,執行後會得之後的結果。
$ python osscmd gs --id=your_id --key=your_key --host=your_endpoint 2013-07-19 08:11 test-oss-sample Bucket Number is: 1
如果要配置用戶的ID和KEY到預設的檔案中,請運行如下命令用來配置訪問OSS所需要的ID和KEY。預設的OSS HOST為oss.aliyuncs.com。
$python osscmd config --id=your_id --key=your_key --host=your_endpoint
如果出現類似“Your configuration is saved into ”的提示表明ID和KEY已經保存成功了。
基礎操作
- 列出建立的bucket
$python osscmd getallbucket
如果是剛剛使用OSS的用戶因為沒有建立bucket,輸出是空。
- 建立bucket
建立一個Bucket名字為mybucketname的bucket。
$python osscmd createbucket mybucketname
建立“mybucketname”的bucket,有可能不成功。因為OSS中的bucket名字是全域唯一的,並且有人已經建立了這個bucket。這個時候需要換一個名字。例如在bucket名字中加入特定的日期。
- 查看是否建立成功
$python osscmd getallbucket
如果沒有成功請檢查osscmd返回的錯誤資訊。
- 查看Object
成功建立bucket後,查看bucket中有哪些object。
$python osscmd list oss://mybucketname/
由於bucket中還沒有object,輸出是空的。
- 上傳object
向bucket中上傳一個object。假如本地檔案名叫local_existed_file,其MD5值如下所示。
$ md5sum local_existed_file 7625e1adc3a4b129763d580ca0a78e44 local_existed_file $ python osscmd put local_existed_file oss://mybucketname/test_object
说明 md5sum
為 Linux 命令, Windows下無此命令。 - 再次查看Object
如果建立成功,再次查看bucket中有哪些object。
$python osscmd list oss://mybucketname/
- 下載object
從bucket中下載object到本地檔案,並比對下載的檔案的md5值
$ python osscmd get oss://mybucketname/test_object download_file $ md5sum download_file 7625e1adc3a4b129763d580ca0a78e44 download_file
说明 md5sum
為 Linux 命令, Windows下無此命令。 - 刪除object
$ python osscmd delete oss://mybucketname/test_object
- 刪除bucket
说明 如果bucket中還有object的話則這個bucket不能被刪除。 $ python osscmd deletebucket mybucketname
使用lifecycle
- 配置一個lifecycle的xml格式的文字檔
<LifecycleConfiguration> <Rule> <ID>1125</ID> <Prefix>log_backup/</Prefix> <Status>Enabled</Status> <Expiration> <Days>2</Days> </Expiration> </Rule> </LifecycleConfiguration>
這個表示刪除bucket下,以log_backup/ 為首碼的,並且相對目前時間超過2天的object。詳細的規則配置可以參考API文檔
- 寫入lifecycle
python osscmd putlifecycle oss://mybucket lifecycle.xml 0.150(s) elapsed
- 讀取lifecycle
python osscmd getlifecycle oss://mybucket <?xml version="1.0" encoding="UTF-8"?> <LifecycleConfiguration> <Rule> <ID>1125</ID> <Prefix>log_backup/</Prefix> <Status>Enabled</Status> <Expiration> <Days>2</Days> </Expiration> </Rule> </LifecycleConfiguration> 0.027(s) elapsed
- 刪除lifecycle
python osscmd deletelifecycle oss://mybucket 0.139(s) elapsed
- 讀取lifecyle
python osscmd getlifecycle oss://mybucket Error Headers: [('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')] Error Body: <?xml version="1.0" encoding="UTF-8"?> <Error> <BucketName>mybucket</BucketName> <Code>NoSuchLifecycle</Code> <Message>No Row found in Lifecycle Table.</Message> <RequestId>54C74FEE5D7F6B24E5042630</RequestId> <HostId>mybucket.oss-maque-hz-a.alibaba.net</HostId> </Error> Error Status: 404 getlifecycle Failed!
防盜鏈設定
- 允許空referer訪問
$osscmd putreferer oss://test --allow_empty_referer=true 0.004(s) elapsed
- 獲取設定的referer
$osscmd getreferer oss://test <?xml version="1.0" encoding="UTF-8"?> <RefererConfiguration> <AllowEmptyReferer>true</AllowEmptyReferer> <RefererList /> </RefererConfiguration>
- 不允許空referer,只允許referer為test的請求
$osscmd putreferer oss://test --allow_empty_referer=false --referer='www.test.com' 0.092(s) elapsed
- 獲取設定的referer
$osscmd getreferer oss://test <?xml version="1.0" encoding="UTF-8"?> <RefererConfiguration> <AllowEmptyReferer>false</AllowEmptyReferer> <RefererList> <Referer>www.test.com</Referer> </RefererList> </RefererConfiguration>
- 不允許空referer,只允許referer為test和test1的請求
$osscmd putreferer oss://test --allow_empty_referer=false --referer='www.test.com,www.test1.com'
- 獲取設定的referer
$osscmd getreferer oss://test <?xml version="1.0" encoding="UTF-8"?> <RefererConfiguration> <AllowEmptyReferer>false</AllowEmptyReferer> <RefererList> <Referer>www.test.com</Referer> <Referer>www.test1.com</Referer> </RefererList> </RefererConfiguration>
使用logging
- 設定logging
$osscmd putlogging oss://mybucket oss://myloggingbucket/mb
- 獲取設定的logging
$osscmd getlogging oss://mybucket