很方便操作自己的存储空间
 
1: #coding=utf8
   2: from oss_api import *
   3: from oss_xml_handler import *
   4: import os
   5: import sys
   6:  
   7: HOST="storage.aliyun.com"
   8: ACCESS_ID = ""
   9: SECRET_ACCESS_KEY = ""
  10:  
  11: class OssFS:
  12:     def __init__(self, oHost, oId="", oKey=""):
  13:         self.oHost = oHost
  14:         self.oId = oId
  15:         self.oKey = oKey
  16:         self.oss = OssAPI(oHost,oId,oKey)
  17:         self.buckets = []
  18:         res = self.oss.list_all_my_buckets()
  19:         http_body = res.read()
  20:         if 2 == (res.status/100):
  21:             bucket_list = GetServiceXml(http_body)
  22:             for bucket in bucket_list.list():
  23:                 (h1,h2) = bucket
  24:                 self.buckets.append(str(h1))
  25:         else:
  26:                 print http_body
  27:  
  28:     def show_buckets(self):
  29:         '''Show all buckets'''
  30:         for each in self.buckets:
  31:             print each
  32:  
  33:     def sizetoKMGT(self,snum):
  34:         '''convert size number to KB/MB/GB/TB'''
  35:         if 0 == int(snum):
  36:             return "obj grp"
  37:         lsize = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  38:         i = 0
  39:         snum = int(snum) * 1.0
  40:         while snum >; 1024:
  41:             snum = snum / 1024.0
  42:             i  = 1;
  43:         return ('%.2f'   " "  lsize)%(snum)
  44:  
  45:  
  46:     def show_objects(self, bucket = '', path = ''):
  47:         '''Show all objects in the path from the bucket'''
  48:         res = self.oss.list_bucket(bucket)
  49:         http_body = res.read()
  50:         if 2 == (res.status / 100):
  51:             h = GetBucketXml(http_body)
  52:             (file_list ,common_list) = h.list()
  53:             for each in file_list:
  54:                 (fname, ctime, etag, fsize, owner, owner2, fstyle) = each
  55:                 print "s\t %s\t %s" % (str(fname),self.sizetoKMGT(fsize),etag)
  56:         else:
  57:             print http_body
  58:  
  59:     def file_upload(self, bucket='' ,path='' ,file_name=''):
  60:         '''Upload a file from local PC to the OSS'''
  61:         res = self.oss.put_object_from_file(bucket, file_name, file_name)
  62:         if 2 == (res.status/100): 
  63:             print "Success to upload the file: "   file_name
  64:             return True
  65:         else:
  66:             print res.read()
  67:     
  68:     def is_file(self, bucket, file_name):
  69:         '''Wether the file_name is in the bucket or not'''
  70:         res = self.oss.list_bucket(bucket)
  71:         http_body = res.read()
  72:         if 2 ==(res.status / 100):
  73:             h = GetBucketXml(http_body)
  74:             (file_list, common_list) = h.list()
  75:             for each in file_list:
  76:                 fname = str(each[0])
  77:                 if fname == file_name:
  78:                     return True
  79:             return False
  80:  
  81:     def file_download(self, bucket, file_name):
  82:         '''Download the file(file_name) from the bucket'''
  83:         res = self.get_object_to_file(bucket, file_name, file_name)
  84:         if 2 == (res.status / 100):
  85:             print "Get file: " file_name
  86:         else:
  87:             print res.read()
  88:     
  89:     def file_delete(self, bucket, file_name):
  90:         '''Delete the file(file_name) from the bucket'''
  91:         res = self.oss.object_operation("DELETE", bucket, file_name)
  92:         if 2 == (res.status/100):
  93:             print "Success to delete the file: "   file_name
  94:             return True
  95:         else:
  96:             print res.read()
  97:             return False
  98:  
  99: if __name__=="__main__":
 100:     isRoot = True;
 101:     #isRoot wether is root floder
 102:     uBucket = ''
 103:     uPath = ''
 104:     uFs = OssFS(HOST,ACCESS_ID,SECRET_ACCESS_KEY)
 105:  
 106:     print "==============================================="
 107:     while True:
 108:         strCmd = raw_input("Input your command:")
 109:         cmd = strCmd.split(' ')
 110:  
 111:         if "ls" == cmd[0]:
 112:             if 1 == len(cmd):
 113:                 if True == isRoot:
 114:                     uFs.show_buckets()
 115:                     continue
 116:                 else:
 117:                     uFs.show_objects(uBucket,uPath)
 118:                     continue
 119:             elif 2 == len(cmd):
 120:                 if "local" == cmd[1]:
 121:                     for each in os.listdir('.'):
 122:                         print each
 123:                     continue
 124:                 else:
 125:                     print ">>>ERROR: BAD option! (Try \"ls local\")"
 126:                     continue
 127:  
 128:         elif "cd" == cmd[0]:
 129:             if len(cmd) != 2:
 130:                 print ">>>ERROR: BAD option! (Bucket name or file name need)"
 131:                 continue
 132:             else:
 133:                 if '.' == cmd[1]:
 134:                     continue
 135:                 elif '..' == cmd[1]:
 136:                     if True == isRoot:
 137:                         print "Root already"
 138:                     else:
 139:                         isRoot = True
 140:                         continue
 141:                 else:
 142:                     if False == isRoot:
 143:                         print ">>>ERROR BAD bucket, you are in bucket already"
 144:                         continue
 145:                     uBucket = cmd[1]
 146:                     if uBucket not in uFs.buckets:
 147:                         print ">>>ERROR BAD bucket" uBucket " is not in your buckets"
 148:                         continue
 149:                     isRoot = False
 150:                     continue
 151:  
 152:         elif "put" == cmd[0]:
 153:             if True == isRoot:
 154:                 print ">>>ERROR: BAD PATH (you must cd a bucket first )"
 155:                 continue
 156:             if len(cmd) != 2:
 157:                 print ">>>ERROR: BAD option! (put a file one time)"
 158:                 continue
 159:             else:
 160:                 file_name = cmd[1]
 161:                 if os.path.isfile(file_name):                    #file exist
 162:                     uFs.file_upload(uBucket,uPath,file_name)
 163:                     continue
 164:                 else:
 165:                     print ">>>ERROR: BAD file name (file not exist!)"
 166:                     continue
 167:  
 168:         elif "get" == cmd[0]:
 169:             if len(cmd) !=2:
 170:                 print ">>>ERROR: BAD option"
 171:                 continue
 172:             if True == isRoot:
 173:                 print ">>>ERROR: BAD get, access a bucket first"
 174:                 continue
 175:             file_name = cmd[1]
 176:             if False == uFs.is_file(uBucket, file_name):
 177:                 print file_name   " is not in your bucket"   uBucket
 178:                 continue
 179:             uFs.file_download(uBacket,file_name)    #didnot consider the file has already exist
 180:             continue
 181:         
 182:         elif "delete" == cmd[0]:
 183:             if True == isRoot:
 184:                 print ">>>BAD delete, I will not let you delete your bucket!"
 185:                 continue
 186:             if len(cmd) != 2:
 187:                 print ">>>BAD options"
 188:                 continue
 189:             file_name = cmd[1]
 190:             if False == uFs.is_file(uBucket, file_name):
 191:                 print file_name   " is not in your bucket"   uBucket
 192:                 continue
 193:             uFs.file_delete(uBucket, file_name)
 194:             continue
 195:             
 196:         elif "help" == cmd[0]:
 197:             pass
 198:             continue
 199:  
 200:         elif "quit" == cmd[0]:
 201:             break
 202:  
 203:         else:
 204:             print "Unable to recognize your command!"
 205:             continue
 206:     print "==============================================="
 
原文有些长就不贴了
http: //www.dullgull.com/2012/07/阿里云存储服务oss-api(python)/
 
 
 
 
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。