开发者社区> 问答> 正文

pythonoss操作报socket错误

代码如下: 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


展开
收起
kernelsec 2014-12-17 17:11:37 11136 0
2 条回答
写回答
取消 提交回答
  • url = "http://" + HOST + "/oss/"

    楼主使用的是一种已经outdate方式,从2012年的文档里面已经不出现这种写法。
    现在的url格式都是 bucket.region.aliyuncs.com/object

    region表示方法请见 API手册 3.2节
    http://imgs-storage.cdn.aliyuncs.com/help/oss/oss%20api%2020140828.pdf?spm=5176.383663.9.2.eVypmo&file=oss%20api%2020140828.pdf
    2014-12-22 14:45:32
    赞同 展开评论 打赏
  • Repythonoss操作报socket错误
    楼主可以去工单系统提问下吧  
    2014-12-18 15:18:39
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载