current position:Home>Detailed tutorial on Python operation Tencent object storage (COS)

Detailed tutorial on Python operation Tencent object storage (COS)

2022-02-01 03:38:43 ruochen

「 This is my participation 11 The fourth of the yuegengwen challenge 21 God , Check out the activity details :2021 One last more challenge

1. Tencent object storage

1.1 Opening service

1.2 backstage

 Insert picture description here

1.3 Creating buckets

 Insert picture description here

1.4 Upload files and view

  • Upload files

 Insert picture description here

  • After the upload , Click on the details , After entering, you will see an object address , Copy opens in the browser to view the file

 Insert picture description here

 Insert picture description here

“ bucket ” The concept of can be understood as an area , Or a folder , Capable of data access

2. python Upload files

Click on Overview , We can see SDK file , Open search python SDK file  Insert picture description here

pip install -U cos-python-sdk-v5
 Copy code 
# -*- coding=utf-8
# appid  Removed from configuration , Please enter the parameter  Bucket  On the middle belt  appid.Bucket  from  BucketName-APPID  form 
# 1.  Set user configuration ,  Include  secretId,secretKey  as well as  Region
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys

secret_id = 'COS_SECRETID'      #  Replace with the user's  secretId
secret_key = 'COS_SECRETKEY'      #  Replace with the user's  secretKey
region = 'ap-nanjing'     #  Replace with the user's  Region

token = None                #  Using a temporary key requires passing in  Token, The default is empty. , Not required 
scheme = 'https'            #  Specify the use of  http/https  Protocol to access  COS, The default is  https, Not required 
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
# 2.  Get client object 
client = CosS3Client(config)
 Copy code 

secret_id & secret_key

 Insert picture description here

 Insert picture description here

 Insert picture description here

region: Area

 Insert picture description here

#  Creating buckets 
response = client.create_bucket(
    #  The name of the bucket 
    Bucket='ruochen-1301954372'
)
 Copy code 

 Insert picture description here

#  Upload files 
response = client.upload_file(
    Bucket='ruochen-1301954372',
    LocalFilePath='local.txt',  #  Path to local file 
    Key='picture.jpg',  #  The file name after uploading to the bucket 
    PartSize=1,  #  Upload is divided into several parts 
    MAXThread=10,  #  Maximum number of threads supported 
    EnableMD5=False  #  Do you support MD5
)
print(response['ETag'])
 Copy code 

2.1 Upload file sample code

# -*- coding: UTF-8 -*-
'''================================================= @Project -> File :MyDjango -> cos_upload_demo @IDE :PyCharm @Author :ruochen @Date :2020/7/17 11:52 @Desc : =================================================='''
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client

secret_id = ' own id'  #  Replace with the user's  secretId
secret_key = ' own key'  #  Replace with the user's  secretKey
region = 'ap-nanjing'  #  Replace with the user's  Region

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
client = CosS3Client(config)

response = client.upload_file(
    Bucket='ruochen-1301954372',
    LocalFilePath='code.png',  #  Path to local file 
    Key='p1.jpg',  #  The file name after uploading to the bucket 
)
print(response['ETag'])
 Copy code 

 Insert picture description here

2.2 Create bucket sample code

# -*- coding: UTF-8 -*-
'''================================================= @Project -> File :MyDjango -> cos_upload_demo @IDE :PyCharm @Author :ruochen @Date :2020/7/17 11:52 @Desc : =================================================='''
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client

secret_id = ' own id'  #  Replace with the user's  secretId
secret_key = ' own key'  #  Replace with the user's  secretKey
region = 'ap-nanjing'  #  Replace with the user's  Region

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
client = CosS3Client(config)

response = client.create_bucket(
    Bucket='test-1301954372',
    ACL='public-read',  # private / public-read / public-read-wirte
)
 Copy code 

 Insert picture description here

Last , Welcome to my personal WeChat official account  『 Little ape like dust 』, For more IT technology 、 Dry goods knowledge 、 Hot news

copyright notice
author[ruochen],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010338416126.html

Random recommended