Python 上传图片到 OSS

时间:2024-04-12 12:18:43
# -*- coding: utf-8 -*-
import oss2, uuid


def oss(img_key, img_path):

    auth = oss2.Auth('LTA。。。。。。', '3XkJh。。。。。。')
    endpoint = 'http://oss-cn-qingdao.aliyuncs.com'
    bucket = oss2.Bucket(auth, endpoint, 'dasheng1104')
    bucket.put_object_from_file(img_key, img_path)

if __name__=="__main__":
    img_key = str(uuid.uuid4()) + '.jpg'
    img_path = '/Users/kunlun/Downloads/purchase_process_1.png'
    oss(img_key, img_path)
    print "upload success!"

 

###

  1. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
  2. bucket = oss2.Bucket(auth, 'http://oss-cn-qingdao.aliyuncs.com', '<yourBucketName>')
  3. img_key = str(uuid.uuid4()) + '.jpg'  为了不让其重复

    Python 上传图片到 OSS