Ali cloud OSS, python SDK QuickStart, including creating storage space, upload, download, list, delete files, etc.

# -*- coding: utf-8 -*-
   
  import os
  import shutil
   
  import oss2
   
   
  # The following code shows the basic file upload, download, list, delete usage.
   
   
  # First initialize AccessKeyId, AccessKeySecret, Endpoint and other information.
  # Get through the environment variable, or the like "<your AccessKeyId>" replaced with real AccessKeyId and so on.
  #
  # In Hangzhou area, for example, Endpoint can be:
  # http://oss-cn-hangzhou.aliyuncs.com
  # https://oss-cn-hangzhou.aliyuncs.com
  # Separate visits to HTTP, HTTPS protocol.
  access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '<你的AccessKeyId>')
  access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '<你的AccessKeySecret>')
  bucket_name = os.getenv('OSS_TEST_BUCKET', '<你的Bucket>')
  Endpoint = os.getenv ( 'OSS_TEST_ENDPOINT ', '<your domain name visit> ')
   
   
  # To confirm the above parameters are correctly filled up
  for param in (access_key_id, access_key_secret, bucket_name, endpoint):
  Assert '< ' Not in param, 'set the parameters: ' + param
   
   
  # Create a Bucket Objects, Object all relevant interfaces can be carried out by Bucket objects
  bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
   
   
  # Upload a string. Object name is motto.txt, was a famous content.
  bucket.put_object('motto.txt', 'Never give up. - Jack Ma')
   
  # Of obtaining metadata Object
  object_meta = bucket.get_object_meta ( 'your object name ')
  print('last modified: ' + object_meta.last_modified)
  print('etag: ' + object_meta.etag)
  print('size: ' + object_meta.content_length)
   
  # 下载到本地文件
  bucket.get_object_to_file('motto.txt', '本地文件名.txt')
   
   
  # 把刚刚上传的Object下载到本地文件 “座右铭.txt” 中
  # 因为get_object()方法返回的是一个file-like object,所以我们可以直接用shutil.copyfileobj()做拷贝
  with open(oss2.to_unicode('本地座右铭.txt'), 'wb') as f:
  shutil.copyfileobj(bucket.get_object('motto.txt'), f)
   
   
  # 把本地文件 “座右铭.txt” 上传到OSS,新的Object叫做 “我的座右铭.txt”
  # 注意到,这次put_object()的第二个参数是file object;而上次上传是一个字符串。
  # put_object()能够识别不同的参数类型
  with open(oss2.to_unicode('本地座右铭.txt'), 'rb') as f:
  bucket.put_object('云上座右铭.txt', f)
   
   
  # 上面两行代码,也可以用下面的一行代码来实现
  bucket.put_object_from_file('云上座右铭.txt', '本地座右铭.txt')
   
   
  # 列举Bucket下10个Object,并打印它们的最后修改时间、文件名
  for i, object_info in enumerate(oss2.ObjectIterator(bucket)):
  print("{0} {1}".format(object_info.last_modified, object_info.key))
   
  if i >= 9:
  break
   
   
  # 删除名为motto.txt的Object
  bucket.delete_object('motto.txt')
   
  # 也可以批量删除
  # 注意:重复删除motto.txt,并不会报错
  bucket.batch_delete_objects(['motto.txt', '云上座右铭.txt'])
   
   
  # 确认Object已经被删除了
  assert not bucket.object_exists('motto.txt')
   
   
  # 获取不存在的文件会抛出oss2.exceptions.NoSuchKey异常
  try:
  bucket.get_object('云上座右铭.txt')
  except oss2.exceptions.NoSuchKey as e:
  print(u'已经被删除了:request_id={0}'.format(e.request_id))
  else:
  assert False
   
  # 清除本地文件
  os.remove(u'本地文件名.txt')
  os.remove(u'本地座右铭.txt')

Guess you like

Origin www.cnblogs.com/fangjianyi/p/11108858.html