#!/usr/bin/env python3

# -*- coding: utf-8 -*-
# @mail    : lshan523@163.com
# @Time    : 2024/12/13 14:16
# @Author  : Sea
# @File    : uploadOss.py# @Purpose :
# @history : pip install oss2
# ****************************
# -*- coding: utf-8 -*-
import logging
import os
from itertools import islice
import oss2
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

accessKeyId = 'LTAIXXXXXXXXTVVvVpc'
accessKeySecret = 'FXXXXXXXXXXXxXXXXXXXCQls'
auth = oss2.Auth(accessKeyId, accessKeySecret)
# 设置Endpoint和Region
endpoint = "https://oss-cn-chengdu.aliyuncs.com"
region = "cn-chengdu"
bucket_name = "mybucketname"
bucket = oss2.Bucket(auth, endpoint, bucket_name, region=region) def create_bucket(bucket): try: bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE) logging.info("Bucket created successfully") except oss2.exceptions.OssError as e: logging.error(f"Failed to create bucket: {e}") def upload_file(bucket, object_name, filePath): try: # result = bucket.put_object(object_name, data) result = bucket.put_object_from_file(object_name, filePath) logging.info(f"File uploaded successfully, status code: {result.status}") except oss2.exceptions.OssError as e: logging.error(f"Failed to upload file: {e}") def upload_file_io(bucket, object_name, data): try: result = bucket.put_object(object_name, data) # result = bucket.put_object_from_file(object_name, filePath) logging.info(f"File uploaded successfully, status code: {result.status}") except oss2.exceptions.OssError as e: logging.error(f"Failed to upload file: {e}") def download_file(bucket, object_name): try: file_obj = bucket.get_object(object_name) content = file_obj.read().decode('utf-8') logging.info("File content:") logging.info(content) return content except oss2.exceptions.OssError as e: logging.error(f"Failed to download file: {e}") def list_objects(bucket): try: objects = list(islice(oss2.ObjectIterator(bucket), 10)) for obj in objects: logging.info(obj.key) except oss2.exceptions.OssError as e: logging.error(f"Failed to list objects: {e}") def delete_objects(bucket): try: objects = list(islice(oss2.ObjectIterator(bucket), 100)) if objects: for obj in objects: bucket.delete_object(obj.key) logging.info(f"Deleted object: {obj.key}") else: logging.info("No objects to delete") except oss2.exceptions.OssError as e: logging.error(f"Failed to delete objects: {e}") def delete_bucket(bucket): try: bucket.delete_bucket() logging.info("Bucket deleted successfully") except oss2.exceptions.OssError as e: logging.error(f"Failed to delete bucket: {e}") def upload_folder(folder_path,oss_path): # folder_path = 'C:/Users/top/Downloads/grid5' # 列出文件夹下的所有文件和子文件夹 for filename in os.listdir(folder_path): if os.path.isfile(os.path.join(folder_path, filename)): print(filename) file = upload_file(bucket, oss_path+"/" + filename, folder_path + "/" + filename) print("upload file " + filename + " ok " + str(file)) # 主流程 if __name__ == '__main__': pass # # 1. 创建Bucket # create_bucket(bucket) # # 2. 上传文件 # upload_file(bucket, 'test-string-file', b'Hello OSS, this is a test string.') # # 3. 下载文件 # download_file(bucket, 'test-string-file') # # 4. 列出Bucket中的对象 # list_objects(bucket) # # 5. 删除Bucket中的对象 # delete_objects(bucket) # # 6. 删除Bucket # delete_bucket(bucket) # upload_folder("C:/Users/top/Downloads/grid5")

 

posted on 2024-12-13 15:15  lshan  阅读(2)  评论(0编辑  收藏  举报