DESCRIPTION.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. Alibaba Cloud OSS SDK for Python
  2. ================================
  3. .. image:: https://badge.fury.io/py/oss2.svg
  4. :target: https://badge.fury.io/py/oss2
  5. .. image:: https://travis-ci.org/aliyun/aliyun-oss-python-sdk.svg?branch=master
  6. :target: https://travis-ci.org/aliyun/aliyun-oss-python-sdk
  7. .. image:: https://coveralls.io/repos/github/aliyun/aliyun-oss-python-sdk/badge.svg?branch=master
  8. :target: https://coveralls.io/github/aliyun/aliyun-oss-python-sdk?branch=master
  9. `README of Chinese <https://github.com/aliyun/aliyun-oss-python-sdk/blob/master/README-CN.rst>`
  10. Overview
  11. --------
  12. Alibaba Cloud Object Storage Python SDK 2.x. This version is not compatible with the previous version (Version 0.x). The package name is `oss2` to avoid conflict with previous versions.
  13. The SDK of this version is dependent on the third-party HTTP library `requests <https://github.com/kennethreitz/requests>`_ and `crcmod`. Install the SDK following the methods below.
  14. Note:
  15. This version does not contain the `osscmd` command line tool.
  16. Running environment
  17. -------------------
  18. Python 2.6(not recommended),2.7,3.3(not recommended),3.4,3.5,3.6
  19. Note:
  20. Python 2.6 is not recommended because it is no longer supported by the Python core team.
  21. Do not use Python 3.3.0 or 3.3.1. Refer to `Python Issue 16658 <https://bugs.python.org/issue16658>`_.
  22. Installing
  23. ----------
  24. Install the official release version through PIP (taking Linux as an example):
  25. .. code-block:: bash
  26. $ pip install oss2
  27. You can also install the unzipped installer package directly:
  28. .. code-block:: bash
  29. $ sudo python setup.py install
  30. Getting started
  31. ---------------
  32. .. code-block:: python
  33. # -*- coding: utf-8 -*-
  34. import oss2
  35. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # Suppose that your bucket is in the Hangzhou region.
  36. auth = oss2.Auth('<Your AccessKeyID>', '<Your AccessKeySecret>')
  37. bucket = oss2.Bucket(auth, endpoint, '<your bucket name>')
  38. # The object key in the bucket is story.txt
  39. key = 'story.txt'
  40. # Upload
  41. bucket.put_object(key, 'Ali Baba is a happy youth.')
  42. # Download
  43. bucket.get_object(key).read()
  44. # Delete
  45. bucket.delete_object(key)
  46. # Traverse all objects in the bucket
  47. for object_info in oss2.ObjectIterator(bucket):
  48. print(object_info.key)
  49. For more examples, refer to the code under the "examples" directory.
  50. Handling errors
  51. ---------------
  52. The Python SDK interface will throw an exception in case of an error (see oss2.exceptions sub-module) unless otherwise specified. An example is provided below:
  53. .. code-block:: python
  54. try:
  55. result = bucket.get_object(key)
  56. print(result.read())
  57. except oss2.exceptions.NoSuchKey as e:
  58. print('{0} not found: http_status={1}, request_id={2}'.format(key, e.status, e.request_id))
  59. Setup Logging
  60. ---------------
  61. The following code can set the logging level of 'oss2'.
  62. .. code-block:: python
  63. import logging
  64. logging.getLogger('oss2').setLevel(logging.WARNING)
  65. Testing
  66. -------
  67. First set the required AccessKeyId, AccessKeySecret, endpoint and bucket information for the test through environment variables (**Do not use the bucket for the production environment**).
  68. Take the Linux system for example:
  69. .. code-block:: bash
  70. $ export OSS_TEST_ACCESS_KEY_ID=<AccessKeyId>
  71. $ export OSS_TEST_ACCESS_KEY_SECRET=<AccessKeySecret>
  72. $ export OSS_TEST_ENDPOINT=<endpoint>
  73. $ export OSS_TEST_BUCKET=<bucket>
  74. $ export OSS_TEST_STS_ID=<AccessKeyId for testing STS>
  75. $ export OSS_TEST_STS_KEY=<AccessKeySecret for testing STS>
  76. $ export OSS_TEST_STS_ARN=<Role ARN for testing STS>
  77. Run the test in the following method:
  78. .. code-block:: bash
  79. $ nosetests # First install nose
  80. You can set environment variable to test auth v2:
  81. .. code-block:: bash
  82. $ export OSS_TEST_AUTH_VERSION=v2
  83. More resources
  84. --------------
  85. - `More examples <https://github.com/aliyun/aliyun-oss-python-sdk/tree/master/examples>`_.
  86. - `Python SDK API documentation <http://aliyun-oss-python-sdk.readthedocs.org/en/latest>`_.
  87. - `Official Python SDK documentation <https://help.aliyun.com/document_detail/32026.html>`_.
  88. Contacting us
  89. -------------
  90. - `Alibaba Cloud OSS official website <http://oss.aliyun.com>`_.
  91. - `Alibaba Cloud OSS official forum <http://bbs.aliyun.com>`_.
  92. - `Alibaba Cloud OSS official documentation center <https://help.aliyun.com/document_detail/32026.html>`_.
  93. - Alibaba Cloud official technical support: `Submit a ticket <https://workorder.console.aliyun.com/#/ticket/createIndex>`_.
  94. License
  95. -------
  96. - `MIT <https://github.com/aliyun/aliyun-oss-python-sdk/blob/master/LICENSE>`_.