首页 > 其他分享 >aliyun oss pyhton

aliyun oss pyhton

时间:2024-12-13 15:25:09浏览次数:6  
标签:logging aliyun oss object bucket oss2 pyhton objects file

 #!/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")

 

标签:logging,aliyun,oss,object,bucket,oss2,pyhton,objects,file
From: https://www.cnblogs.com/lshan/p/18605037

相关文章

  • 自定义阿里云OSS图片大小
    在访问oss图片时,如果图片太大,而本地网络不是很好的情况下,图片加载时间比较长,阿里云的oss有这个功能,在访问图片的时候指定大小显示,在显示的时候直接对图片进行压缩,这样可以加快访问速度!##图片地址https://gateon-ds.oss-cn-shanghai.aliyuncs.com/000a960090b343f0884b35acbd80e......
  • AEC论文解读 -- MULTI-TASK DEEP RESIDUAL ECHO SUPPRESSION WITH ECHO-AWARE LOSS
    一、技术解读1.1数据处理的方式  使用3带有限脉冲响应(FIR)滤波器组和离散余弦变换(DCT)调制,将全频带(48kHz)信号分解为子带,并且只处理宽带(16kHz)信号,最终全频带信号通过平均增益控制合成。1.1.1信号分解与DCT调制  在信号处理的初始阶段,使用带通FIR滤波器将全频带信号分......
  • 独家原创 | BiTCN-BiGRU-CrossAttention融合时空特征的高创新预测模型
    往期精彩内容:时序预测:LSTM、ARIMA、Holt-Winters、SARIMA模型的分析与比较全是干货|数据集、学习资料、建模资源分享!EMD变体分解效果最好算法——CEEMDAN(五)-CSDN博客拒绝信息泄露!VMD滚动分解+Informer-BiLSTM并行预测模型-CSDN博客单步预测-风速预测模型代码全家桶-......
  • 【人工智能】Moss-AI编程利器:CodeMoss & ChatGPT中文版超详细入门教程!(VScode/IDER/WE
    文章目录摘要一、环境介绍VSvode安装步骤IDER(Pycharm)安装步骤Web使用步骤二、Moss9大功能讲解1、AI问答对话2、文件上传功能3、自定义AI助手4、AI联网助手5、AI图片识别6、思维链思维链的简单介绍使用CodeMoss思维链7、AI图片生成图片生成效果8、图片生成代码9、......
  • 华为OD E卷(100分)02-Boss 的收入
    前言    工作了十几年,从普通的研发工程师一路成长为研发经理、研发总监。临近40岁,本想辞职后换一个相对稳定的工作环境一直干到老,没想到离职后三个多月了还没找到工作,愁肠百结。为了让自己有点事情做,也算提高一下自己的编程能力,无聊之余打算用一些大厂的编程题练练......
  • 录音功能iosSDK
    //// Fast.swift// FastSwiftKit//// CreatedbyXXon2023/9/7.// importAVFoundation publicclassFastAudioManager{     publicstaticletshared=FastAudioManager()     //MARK:音频录制  privatevaraudioRecorder:A......
  • linux服务器目录挂载到oss
    场景之前的文件上传是直接上传的云服务器上的某个目录下,由于用户上传的文件数量越来越多导致云盘快满了。想要在尽量不更改之前文件上传代码的逻辑上解决这个问题。解决方法把之前用户上传文件的目录挂载到oss中,上传文件的代码只需要把文件的域名改为oss的域名即可。操作步骤......
  • 【Thinkphp6】使用框架内置中间件AllowCrossDomain允许跨域请求,当请求header中携带自
    【问题描述】使用框架内置中间件AllowCrossDomain允许跨域请求,当请求header中携带自定义参数时,跨域请求报错,不携带自定义参数时,可正常访问【解决方案】方案一:修改框架内置中间件修改框架内置中间件 AllowCrossDomain,将$header属性的 Access-Control-Allow-Headers ......
  • 谷歌浏览器strict-origin-when-cross-origin 解决
    问题描述:将Vue前端部署到服务器的Nginx以后,浏览器访问资源时就会产生跨域问题,随后使用gateway做了网关配置。配置完成后使用谷歌浏览器访问资源时控制台报错strict-origin-when-cross-origin随后使用火狐浏览器访问资源时为200而直接在地址栏输入请求url也是200解决思路网站当前访......
  • YOLOv8改进 | 损失函数篇:SlideLoss与FocalLoss的细节优化与应用【YOLOv8】
    本专栏专为AI视觉领域的爱好者和从业者打造。涵盖分类、检测、分割、追踪等多项技术,带你从入门到精通!后续更有实战项目,助你轻松应对面试挑战!立即订阅,开启你的YOLOv8之旅!专栏订阅地址:https://blog.csdn.net/mrdeam/category_12804295.html文章目录YOLOv8改进|损失函数......