zoukankan      html  css  js  c++  java
  • OSS 上传内容

    1. 下载安装 oss2

    pip install oss2

    2. 配置oss连接

    access_key_id = ‘’
    access_key_secret = ‘’
    bucket_name = ‘’
    endpoint = ‘’
    username = ‘’
    ini 配置文件

     2.1 使用 configparser 进行配置文件读取

    import configparser
    config = configparser.ConfigParser()
    config.read('example.ini')   # 读文件
    config.add_section('yuan')   # 增加section
    config.remove_section('bitbucket.org')   # 删除一个section
    config.remove_option('topsecret.server.com',"forwardx11")  # 删除一个配置项
    config.set('topsecret.server.com','k1','11111')
    config.set('yuan','k2','22222')
    f = open('new2.ini', "w")
    config.write(f) # 写进文件
    f.close()

    2.2 设置文件存储类型及访问权限

    # 创建连接对象
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    # 上传文件
    # 表示不包含Bucket名称在内的Object的完整路径
    result = bucket.put_object('<yourObjectName>', 'content of object')
    
    # HTTP返回码。
    print('http status: {0}'.format(result.status))
    # 请求ID。请求ID是请求的唯一标识,强烈建议在程序日志中添加此参数。
    print('request_id: {0}'.format(result.request_id))
    # ETag是put_object方法返回值特有的属性,用于标识一个Object的内容。
    print('ETag: {0}'.format(result.etag))
    # HTTP响应头部。
    print('date: {0}'.format(result.headers['date']))

    # 设置bucket版本控制状态
    
    from oss2.models import BucketVersioningConfig
    
    # 创建bucket版本控制配置。
    config = BucketVersioningConfig()
    # 状态配置为Enabled或Suspended。
    config.status = oss2.BUCKET_VERSIONING_ENABLE
    
    # 设置bucket版本控制状态。
    result = bucket.put_bucket_versioning(config)
    # 查看http返回码。
    print('http response code:', result.status)
    # 获取bucket版本控制状态信息。
    
    versioning_info = bucket.get_bucket_versioning()
    # 查看bucket版本控制状态, 如果曾开启过版本控制则返回Enabled或Suspended, 如果从未开启过版本控制则返回None。
    print('bucket versioning status:', versioning_info.status)
  • 相关阅读:
    At least one JAR was scanned for TLDs yet contained no TLDs
    {转} MJPG流媒体在HTML5的呈现方案
    {转}理解HTTP/304响应
    blueimp,预览遮罩范围控制
    快速生成mysql上百万条测试数据
    mysql插入文本文档及读取
    tomcat启动报错:注释指定的bean类.与现有的冲突.相同的名称和类
    csv的文件excel打开长数字后面位变0的解决方法
    UNIX或LINUX时间戳转时间
    Myeclipse更新SVNStatusSubscriber 时报告了错误。1 中的 0 个资源已经同步。
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/14473109.html
Copyright © 2011-2022 走看看