oss_client.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import oss2
  2. import logging
  3. logging.basicConfig(
  4. format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
  5. logger = logging.getLogger(__name__)
  6. access_key_id="LTAI5tHMkNaRhpiDB1yWMZPn"
  7. access_key_secret="XLi5YUJusVwbbQOaGeGsaRJ1Qyzbui"
  8. auth = oss2.AuthV4(access_key_id, access_key_secret)
  9. hangzhou_config = {
  10. "endpoint" : "https://oss-cn-hangzhou.aliyuncs.com",
  11. "inner_endpoint" : "https://oss-cn-hangzhou-internal.aliyuncs.com",
  12. "region" : "cn-hangzhou"
  13. }
  14. class HangZhouOSSClient:
  15. def __init__(self, bucket_name):
  16. self.bucket_name = bucket_name
  17. self.bucket = oss2.Bucket(auth, hangzhou_config["inner_endpoint"], bucket_name, region=hangzhou_config["region"])
  18. def put_object_from_file(self, object_name, local_file):
  19. result = self.bucket.put_object_from_file(object_name, local_file)
  20. logger.info("\n status: {} \n request_id: {} \n ETag: {} \n date: {}".format(result.status, result.request_id,
  21. result.etag, result.headers['date']))
  22. if __name__ == "__main__":
  23. client = HangZhouOSSClient("art-recommend")
  24. client.put_object_from_file("dyp/stuuudy.pem", "/Users/dingyunpeng/stuuudy.pem")