代码如下:
if __name__ == "__main__":
#初始化
if len(ACCESS_ID) == 0 or len(SECRET_ACCESS_KEY) == 0:
print "Please make sure ACCESS_ID and SECRET_ACCESS_KEY are correct in ", __file__ , ", init are empty!"
exit(0)
oss = OssAPI(HOST, ACCESS_ID, SECRET_ACCESS_KEY) # 创建一个oss实例
sep = "=============================="
#对特定的URL签名,默认URL过期时间为60秒
method = "GET"
bucket = "kernelsec-client" # 特定的容器,bucket
# url = "http://" + HOST + "/oss/" + bucket # host + 指定的bucket
url = "http://" + HOST + "/oss/"
headers = {}
# resource = "/" + bucket
resource = "/"
timeout = 60
url_with_auth = oss.sign_url_auth_with_expire_time(method, url, headers, resource, timeout)
print "after signature url is: ", url_with_auth
print sep
#下载bucket中的object,把内容写入到本地文件中
object = "1.xls"
headers = {}
filename = "2.xls"
res = oss.get_object_to_file(bucket, object, filename, headers) # 下载文件到指定文件名
if (res.status / 100) == 2:
print "2.xls OK"
else:
print "2.xls ERROR"
print sep
# #创建属于自己的bucket
# my_bucket = "test_bucket"
# acl = 'private'
# headers = {}
# res = oss.put_bucket(my_bucket, acl, headers) # 等同create_bucket创建bucket容器
# if (res.status / 100) == 2:
# print "put bucket ", my_bucket, "OK"
# else:
# print "put bucket ", my_bucket, "ERROR"
# print sep
# 列出创建的bucket
res = oss.get_service() # 等同于list_all_my_buckets
if (res.status / 100) == 2:
body = res.read()
h = GetServiceXml(body)
print "bucket list size is: ", len(h.list())
print "bucket list is: "
for i in h.list():
print i
else:
print res.status
print sep
#把指定的字符串内容上传到bucket中,在bucket中的文件名叫object。
object = "object_test"
input_content = "hello, OSS"
content_type = "text/HTML"
headers = {}
res = oss.put_object_from_string(bucket, object, input_content, content_type, headers) # 将指定内容字符串上传为object
if (res.status / 100) == 2:
print "put_object_from_string OK"
else:
print "put_object_from_string ERROR"
print sep
#指定文件名, 把这个文件上传到bucket中,在bucket中的文件名叫object。
object = "1.xls"
filename = "1.xls"
content_type = ""
headers = {}
res = oss.put_object_from_file(bucket, object, filename, content_type, headers) # 将本地文件上传到object
if (res.status / 100) == 2:
print "put_object_from_file OK"
else:
print "put_object_from_file ERROR"
print sep
可以成功get_service但是其他操作就是失败
C:\Python27\python.exe "I:/Python Project/Socket/oss_test.py"
after signature url is: http://oss.aliyuncs.com/oss/?OSSAccessKeyId=eU5a7S6j5BZAGWAA&Expires=1418806982&Signature=%2F%2BVUQ2O6BIn%2BzowMm1yZvQaEyBg%3D
==============================
2.xls ERROR
==============================
bucket list size is: 1
bucket list is:
(u'kernelsec-client', u'2014-12-17T03:29:43.000Z', u'oss-cn-beijing-a')
==============================
put_object_from_string ERROR
==============================
Traceback (most recent call last):
File "I:/Python Project/Socket/oss_test.py", line 95, in <module>
res = oss.put_object_from_file(bucket, object, filename, content_type, headers) # 将本地文件上传到object
File "C:\Python27\lib\site-packages\oss-0.1.3-py2.7.egg\oss\oss_api.py", line 922, in put_object_from_file
res = self.put_object_from_fp(bucket, object, fp, content_type, headers, params)
File "C:\Python27\lib\site-packages\oss-0.1.3-py2.7.egg\oss\oss_api.py", line 984, in put_object_from_fp
return self._put_or_post_object_from_fp(method, bucket, object, fp, content_type, headers, params)
File "C:\Python27\lib\site-packages\oss-0.1.3-py2.7.egg\oss\oss_api.py", line 1041, in _put_or_post_object_from_fp
res = conn.getresponse()
File "C:\Python27\lib\httplib.py", line 1067, in getresponse
response.begin()
File "C:\Python27\lib\httplib.py", line 409, in begin
version, status, reason = self._read_status()
File "C:\Python27\lib\httplib.py", line 365, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "C:\Python27\lib\socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
socket.error: [Errno 10054]
Process finished with exit code 1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。