作者简介
吴硕卫:腾讯云技术支持工程师,现负责腾讯云存储产品的技术支持专项工作。
S3cmd 是免费的命令行工具和客户端,用于在 Amazon S3 和其他兼容 S3 协议的对象存储中上传、下载和管理数据。本文主要介绍如何使用 S3cmd 访问 COS 上的文件。
准备工作
一、使用环境
1、软件依赖
2、安装及配置
二、安装 S3cmd
pip install s3cmd
安装成功之后,用户可以通过--version 命令查看当前的版本信息。
三、配置参数
S3cmd 工具在使用前需要进行参数配置,默认读取 ~/.s3cfg 作为配置文件,可以直接在命令中指定参数,也可以直接通过程序的交互式命令创建配置文件。
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.comUse "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.comUse "%(bucket)s.s3.amazonaws.com" to the target Amazon S3. "%(bucket)s" and "%(location)s" vars can be usedif the target S3 system supports dns based buckets.DNS-style bucket+hostname:port template for accessing a bucket []:
//是否开启HTTPSUse HTTPS protocol []:
//保存设置Save settings? [y/N]
可以直接编辑~/.s3cfg 文件 (在 Windows 环境下,该文件是位于【我的文档】下的一个隐藏文件),该文件初始时不存在,是通过 s3cmd --configure 命令生成,用户也可以手动创建。
配置完成之后的 .s3cfg 文件内容示例如下所示:
[default]#密钥 IDaccess_key = AChT4ThiXAbpBDEFGhT4ThiXAbp****#密钥 Keysecret_key = WE54wreefvds3462refgwewe****#默认地域bucket_location = US#Endpoint接入端点host_base = cos.ap-beijing.myqcloud.com#COS服务接入地址host_bucket = %(bucket)s.cos.ap-beijing.myqcloud.com#是否开启HTTPSuse_https = False
access_token =add_encoding_exts =add_headers =ca_certs_file =cache_file =check_ssl_certificate = Truecheck_ssl_hostname = Truecloudfront_host = cloudfront.amazonaws.comconnection_pooling = Truecontent_disposition =content_type =default_mime_type = binary/octet-streamdelay_updates = Falsedelete_after = Falsedelete_after_fetch = Falsedelete_removed = Falsedry_run = Falseenable_multipart = Trueencrypt = Falseexpiry_date =expiry_days =expiry_prefix =follow_symlinks = Falseforce = Falseget_continue = Falsegpg_command = /usr/bin/gpggpg_decrypt = %(gpg_command)s -d --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)sgpg_encrypt = %(gpg_command)s -c --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)sgpg_passphrase =website_endpoint = http://%(bucket)s.s3-website-%(location)s.amazonaws.com/guess_mime_type = Truehuman_readable_sizes = Falseinvalidate_default_index_on_cf = Falseinvalidate_default_index_root_on_cf = Trueinvalidate_on_cf = Falsekms_key =limit = -1limitrate = 0list_md5 = Falselog_target_prefix =long_listing = Falsemax_delete = -1mime_type =multipart_chunk_size_mb = 15multipart_max_chunks = 10000preserve_attrs = Trueprogress_meter = Trueproxy_host =proxy_port = 0public_url_use_https = Falseput_continue = Falserecursive = Falserecv_chunk = 65536reduced_redundancy = Falserequester_pays = Falserestore_days = 1restore_priority = Standardsend_chunk = 65536server_side_encryption = Falsesignature_v2 = Falsesignurl_use_https = Falsesimpledb_host = sdb.amazonaws.comskip_existing = Falsesocket_timeout = 300stats = Falsestop_on_error = Falsestorage_class =throttle_max = 100upload_id =urlencoding_mode = normaluse_http_expect = Falseuse_mime_magic = Trueverbosity = WARNINGwebsite_error =website_index = index.html
经过如上配置后具体可以操作某一个地域的存储桶。
如有在其它兼容 S3 的工具中使用 COS 可以参考:https://cloud.tencent.com/document/product/436/41284
四、开始使用
下面为您介绍如何使用 s3cmd 完成一个基础操作,例如创建存储桶、查询存储桶列表、上传对象、查询对象列表、下载对象和删除对象。
1、创建存储桶
注意,该存储桶创建时的地域为配置信息里的默认地域
#命令s3cmd mb s3://<BucketName-APPID>#操作示例s3cmd mb s3://examplebucket-1250000000
2、删除存储桶
#命令s3cmd rb s3://<BucketName-APPID>#操作示例s3cmd rb s3://examplebucket-1250000000
3、查询存储桶列表
s3cmd ls
4、查询对象列表
#命令s3cmd ls s3://<BucketName-APPID>#操作示例s3cmd ls s3://examplebucket-1250000000#使用--recursive(或-r)列出所有文件:s3cmd ls s3://examplebucket-1250000000 --recursive
5、上传文件或文件夹
#命令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/ --recursives3cmd put dir1 dir2 s3://examplebucket-1250000000/dir1/ --recursive
6、复制文件或文件夹
#命令s3cmd cp s3://<BucketName-APPID>/<srccospath> s3://<BucketName-APPID>/<destcospath>#操作示例#复制 examplebucket1-1250000000 存储桶下的 exampleobject 对象到 examplebucket2-1250000000 存储桶的 exampleobjects3cmd 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
7、移动文件或文件夹
#命令s3cmd mv s3://<BucketName-APPID>/<srccospath> s3://<BucketName-APPID>/<destcospath>#操作示例#移动 examplebucket21-1250000000 存储桶下的 exampleobject 对象到 examplebucket2-1250000000 存储桶的 exampleobjects3cmd 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
8、下载文件
#命令s3cmd get s3://<BucketName-APPID>/<cospath> <localpath>#操作示例s3cmd put some-file.xml s3://examplebucket-1250000000/exampleobject
9、删除文件或文件夹
#命令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/
10、分块上传文件
#命令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。
11、显示桶内未完成的分块上传
#命令s3cmd multipart s3://<BucketName-APPID>#操作示例s3cmd multipart s3://examplebucket-1250000000
此处会打印出日期、路径以及上传 id
12、查询分块上传文件碎片
#命令s3cmd listmp s3://<BucketName-APPID>/<cospath> UploadID#操作示例s3cmd listmp s3://examplebucket-1250000000 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb
提示 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb 是上传 id,在 s3cmd multipart 中进行查询
13、清除分块上传文件碎片
#命令s3cmd abortmp s3://<BucketName-APPID>/<cospath> UploadID#操作示例s3cmd abortmp s3://examplebucket-1250000000 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb
提示 1586497168ca632296cea7ebe10b43e6c22ab23a067c776d793da1316d8e991aadafd0cebb 是上传 id,在 s3cmd multipart 中进行查询
原创声明,本文系作者授权发表,未经许可,不得转载。
如有侵权,请联系小编删除,谢谢。
点击阅读原文,领取 COS 限时1元礼包!