如何使用 S3CMD 访问 COS 服务

简介

S3cmd 是免费的命令行工具和客户端,用于在 Amazon S3 和其他兼容 S3 协议的对象存储中上传、下载和管理数据。本文主要介绍如何使用 S3cmd 访问 COS 上的文件。

准备工作

  1. 您已注册 腾讯云账号,并且从 访问管理控制台 上获取了腾讯云密钥 SecretID 与 SecretKey。

使用环境

软件依赖

  • Python 2.6+/3+。
  • 最新版本的 pip。

安装及配置

安装 S3cmd

  • 使用 pip 安装(推荐)

pip install s3cmd

安装成功之后,用户可以通过--version命令查看当前的版本信息。

配置参数

S3cmd 工具在使用前需要进行参数配置,默认读取 ~/.s3cfg 作为配置文件,可以直接在命令中指定参数,也可以直接通过程序的交互式命令创建配置文件。

  • 通过 s3cmd --configure 命令来初始化配置信息
Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3. Leave them empty for using the env variables.

//密钥 ID
//例:AChT4ThiXAbpBDEFGhT4ThiXAbp****
Access Key []:

//密钥 Key
//例:WE54wreefvds3462refgwewe****
Secret Key []:

//默认地域
Default Region []:

//这里需要填写COS的地域Endpoint接入地址 cos.<Region>.myqcloud.com
//如cos.ap-beijing.myqcloud.com
Use "s3.amazonaws.com" for S3 Endpoint and not modify it to the target Amazon S3.
S3 Endpoint []:

//COS服务接入地址 : 注意这里有一个 %(bucket)s 参数
//(bucket)s.cos.<region>.myqcloud.com,注意将region替换为您的存储桶所在的地域简称
//例:%"(bucket)s".ap-beijing.myqcloud.com
Use "%(bucket)s.s3.amazonaws.com" to the target Amazon S3. "%(bucket)s" and "%(location)s" vars can be used
if the target S3 system supports dns based buckets.
DNS-style bucket+hostname:port template for accessing a bucket []:

//是否开启HTTPS
Use HTTPS protocol []:

//保存设置
Save settings? [y/N]
  • 编辑 ~/.s3cfg配置文件

可以直接编辑~/.s3cfg 文件 (在 Windows 环境下,该文件是位于【我的文档】下的一个隐藏文件),该文件初始时不存在,是通过 s3cmd --configure 命令生成,用户也可以手动创建。

配置完成之后的.s3cfg文件内容示例如下所示:

[default]
#密钥 ID
access_key = AChT4ThiXAbpBDEFGhT4ThiXAbp****
#密钥 Key
secret_key = WE54wreefvds3462refgwewe****
#默认地域
bucket_location = US
#Endpoint接入端点
host_base = cos.ap-beijing.myqcloud.com
#COS服务接入地址
host_bucket = %(bucket)s.cos.ap-beijing.myqcloud.com
#是否开启HTTPS
use_https = False

access_token =
add_encoding_exts =
add_headers =
ca_certs_file =
cache_file =
check_ssl_certificate = True
check_ssl_hostname = True
cloudfront_host = cloudfront.amazonaws.com
connection_pooling = True
content_disposition =
content_type =
default_mime_type = binary/octet-stream
delay_updates = False
delete_after = False
delete_after_fetch = False
delete_removed = False
dry_run = False
enable_multipart = True
encrypt = False
expiry_date =
expiry_days =
expiry_prefix =
follow_symlinks = False
force = False
get_continue = False
gpg_command = /usr/bin/gpg
gpg_decrypt = %(gpg_command)s -d --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)s
gpg_encrypt = %(gpg_command)s -c --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)s
gpg_passphrase =
website_endpoint = http://%(bucket)s.s3-website-%(location)s.amazonaws.com/
guess_mime_type = True
human_readable_sizes = False
invalidate_default_index_on_cf = False
invalidate_default_index_root_on_cf = True
invalidate_on_cf = False
kms_key =
limit = -1
limitrate = 0
list_md5 = False
log_target_prefix =
long_listing = False
max_delete = -1
mime_type =
multipart_chunk_size_mb = 15
multipart_max_chunks = 10000
preserve_attrs = True
progress_meter = True
proxy_host =
proxy_port = 0
public_url_use_https = False
put_continue = False
recursive = False
recv_chunk = 65536
reduced_redundancy = False
requester_pays = False
restore_days = 1
restore_priority = Standard
send_chunk = 65536
server_side_encryption = False
signature_v2 = False
signurl_use_https = False
simpledb_host = sdb.amazonaws.com
skip_existing = False
socket_timeout = 300
stats = False
stop_on_error = False
storage_class =
throttle_max = 100
upload_id =
urlencoding_mode = normal
use_http_expect = False
use_mime_magic = True
verbosity = WARNING
website_error =
website_index = index.html

经过如上配置后具体可以操作某一个地域的存储桶。

如有在其它兼容S3的工具中使用COS可以参考:https://cloud.tencent.com/document/product/436/41284

开始使用

下面为您介绍如何使用 s3cmd 完成一个基础操作,例如创建存储桶、查询存储桶列表、上传对象、查询对象列表、下载对象和删除对象。

创建存储桶

注意,该存储桶创建时的地域为配置信息里的默认地域

  • 命令如下:
#命令
s3cmd mb s3://<BucketName-APPID>
#操作示例
s3cmd mb s3://examplebucket-1250000000

删除存储桶

  • 命令如下:
#命令
s3cmd rb s3://<BucketName-APPID>
#操作示例
s3cmd rb s3://examplebucket-1250000000

查询存储桶列表

  • 命令如下:
s3cmd ls

查询对象列表

  • 命令如下:
#命令
s3cmd ls s3://<BucketName-APPID>
#操作示例
s3cmd ls s3://examplebucket-1250000000
#使用--recursive(或-r)列出所有文件:
s3cmd ls s3://examplebucket-1250000000 --recursive

上传文件或文件夹

  • 上传文件命令如下:
#命令
s3cmd put <localpath> s3://<BucketName-APPID>/<cospath>
#操作示例
s3cmd put exampleobject s3://examplebucket-1250000000/exampleobject
  • 上传文件夹命令如下:
#命令
s3cmd put --recursive <localdirpath> s3://<BucketName-APPID>/<cosdirpath>
#操作示例
s3cmd put dir1 s3://examplebucket-1250000000/dir1/ --recursive
s3cmd put dir1 dir2 s3://examplebucket-1250000000/dir1/ --recursive

复制文件或文件夹

  • 复制文件命令如下:
#命令
s3cmd cp s3://<BucketName-APPID>/<srccospath> s3://<BucketName-APPID>/<destcospath>
#操作示例
#复制 examplebucket1-1250000000 存储桶下的 exampleobject 对象到 examplebucket2-1250000000 存储桶的 exampleobject
s3cmd cp s3://examplebucket1-1250000000/exampleobject s3://examplebucket2-1250000000/exampleobject
  • 复制文件夹命令如下:
#命令
s3cmd cp s3://<BucketName-APPID>/<srccosdirpath> s3://<BucketName-APPID>/<destcosdirpath> -r
#操作示例
#复制 examplebucket1-1250000000 存储桶下的 examplefolder 目录到 examplebucket2-1250000000 存储桶的 examplefolder 目录
s3cmd cp s3://examplebucket1-1250000000/examplefolder/ s3://examplebucket2-1250000000/examplefolder/ -r

移动文件或文件夹

  • 移动文件命令如下:
#命令
s3cmd mv s3://<BucketName-APPID>/<srccospath> s3://<BucketName-APPID>/<destcospath>
#操作示例
#移动 examplebucket21-1250000000 存储桶下的 exampleobject 对象到 examplebucket2-1250000000 存储桶的 exampleobject
s3cmd mv s3://examplebucket1-1250000000/exampleobject s3://examplebucket2-1250000000/exampleobject
  • 移动文件夹命令如下:
#命令
s3cmd mv s3://<BucketName-APPID>/<srccosdirpath> s3://<BucketName-APPID>/<destcosdirpath> -r
#操作示例
#移动 examplebucket1-1250000000 存储桶下的 examplefolder 目录到 examplebucket2-1250000000 存储桶的 examplefolder 目录
s3cmd mv s3://examplebucket1-1250000000/examplefolder/ s3://examplebucket2-1250000000/examplefolder/ -r

下载文件

  • 下载文件命令如下:
#命令
s3cmd get s3://<BucketName-APPID>/<cospath> <localpath>
#操作示例
s3cmd put some-file.xml s3://examplebucket-1250000000/exampleobject
  • 暂不支持下载文件夹

删除文件或文件夹

  • 删除文件命令如下:
#命令
s3cmd del s3://<BucketName-APPID>/<cospath>
#操作示例
s3cmd del --recursive s3:s3://examplebucket-1250000000/exampleobject
  • 删除文件夹命令如下:
#命令
s3cmd del --recursive s3://<BucketName-APPID>/<cosdirpath>
#操作示例
s3cmd del --recursive s3:s3://examplebucket-1250000000/dir1/

分块上传文件

  • 命令如下:
#命令
s3cmd put <localpath> s3://<BucketName-APPID>/<cospath>  --multipart-chunk-size-mb=5
#操作示例
s3cmd put exampleobject s3://examplebucket-1250000000/exampleobject --multipart-chunk-size-mb=5

提示 multipart-chunk-size-mb 用来设置分块大小,最小值为 5MB, 最大值为 5GB, 默认值为 15MB。

显示桶内未完成的分块上传

  • 命令如下:
#命令
s3cmd multipart s3://<BucketName-APPID>
#操作示例
s3cmd multipart s3://examplebucket-1250000000

此处会打印出日期、路径以及上传 id

查询分块上传文件碎片

  • 命令如下:
#命令
s3cmd listmp s3://<BucketName-APPID>/<cospath> UploadID
#操作示例
s3cmd listmp s3://examplebucket-1250000000 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb

提示 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb 是上传 id,在 s3cmd multipart 中进行查询

清除分块上传文件碎片

  • 命令如下:
#命令
s3cmd abortmp s3://<BucketName-APPID>/<cospath> UploadID
#操作示例
s3cmd abortmp s3://examplebucket-1250000000 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb

提示

1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb 是上传 id,在 s3cmd multipart 中进行查询

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
如何使用 S3CMD 访问 COS 服务
S3cmd 是免费的命令行工具和客户端,用于在 Amazon S3 和其他兼容 S3 协议的对象存储中上传、下载和管理数据。本文主要介绍如何使用 S3cmd 访...
<<上一篇
下一篇>>