本文发布于Cylon的收藏册,转载请著名原文链接~


s3cmd 是一个 Amazon S3 工具,可以用于创建 s3 bucket、向对象存储中上传,检索和管理数据,在下文将如何在 Linux 上如何安装和使用 “s3cmd” 工具。

在 Linux 上安装 s3cmd

s3cmd 在 Ubuntu/Debian, Fedora/CentOS/RHEL 这类发行版上的默认软件包存储库中都是可用的,只需在执行对应发行版的安装命令即可安装。

CentOS/RHEL/Fedora

# centos 8
$ sudo dnf install s3cmd 
# centos 7
$ sudo yum install s3cmd 

Ubuntu/Debian

sudo apt-get install s3cmd

安装最新版本

通常包管理仓库中的版本比较旧,或者使用的 Linux 没有包管理来获取最新版本的 s3cmd,那么可以使用源代码在系统上安装最新版本的 s3cmd,下载地址可以参考附录1 [1]

下面以 2.2 版本进行安装

$ wget https://sourceforge.net/projects/s3tools/files/s3cmd/2.2.0/s3cmd-2.2.0.tar.gz
$ tar xzf s3cmd-2.2.0.tar.gz

使用以下命令和源文件安装

$ cd s3cmd-2.2.0 
$ sudo python setup.py install 

配置 s3cmd

s3cmd 并不仅仅可以管理 AWS s3,也可以管理任意的 S3 对象存储,为了配置 s3cmd 我们需要 Access KeySecret Key 您的 S3 来访问 S3 对象存储,通常 AWS S3 的 Access KeySecret Key 需要到 Amazon security_credential 页面获取 (这里涉及到 AWS 中的用户管理)

使用下列命令配置 s3cmd

s3cmd --configure 

Note:通常这个配置是交互类型的,很多值在自维护的 S3 对象存储中不需要配置,可以一路回车即可

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
Access Key: xxxxxxxxxxxxxxxxxxxxxx
Secret Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: xxxxxxxxxx
Path to GPG program [/usr/bin/gpg]:

When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP and can't be used if you're behind a proxy
Use HTTPS protocol [No]: Yes

New settings:
  Access Key: xxxxxxxxxxxxxxxxxxxxxx
  Secret Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  Encryption password: xxxxxxxxxx
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: True
  HTTP Proxy server name:
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] Y
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Success. Encryption and decryption worked fine :-)

Save settings? [y/N] y
Configuration saved to '/root/.s3cfg'

最终生成的文件在目录 /root/.s3cfg

通常需要关注的参数只有几个

[default]
access_key = <ACCESS KEY FROM PORTAL>
host_base = s3-api.us-geo.objectstorage.softlayer.net
# 这个要注意,在ceph中使用的是下面格式,表示列出的 host/bucket
host_bucket = s3-api.us-geo.objectstorage.softlayer.net/%(bucket)
secret_key = <SECRET KEY LISTED IN PORTAL>

https://gist.github.com/greyhoundforty/a4a9d80a942d22a8a7bf838f7abbcab2

s3cmd examples

说明 命令
列出 bucket 文件 s3cmd ls
创建存储桶 s3cmd mb s3://tecadmin
上传文件到 bucket s3cmd put file.txt s3://tecadmin/
上传目录到 bucket s3cmd put -r backup s3://tecadmin/ 需要注意斜杠才表示目录
下载文件 s3cmd get s3://tecadmin/file.txt
从 bucket 删除文件 s3cmd del s3://tecadmin/file.txt
删除一个目录 s3cmd del s3://tastethelinux/Script
删除 bucket s3cmd rb s3://tastethelinux
拷贝 bucket 文件到另一个 bucket s3cmd cp s3://tastethelinux/tla.txt s3://tastethelinux-example
移动 bucket 文件到另一个 bucket s3cmd mv s3://tastethelinux/tla.txt s3://tastethelinux-example/tla_new.txt
查看存储使用量 s3cmd du s3://tastethelinux/ –human-readable
获取 bucket 信息 s3cmd info s3://tastethelinux
继续上次中断的文件 s3cmd –continue get s3://tastethelinux/tastethelinux.tar.gz
尝试运行但不上传 s3cmd –dry-run
排除规则 —exclude / —include shell 风格通配符 s3cmd sync –dry-run –exclude ‘*.txt’
排除规则 —rexclude / —rinclude 正则表达式 s3cmd sync –dry-run –exclude ‘*.(txt|jpg)’
同步 s3cmd sync ./ s3://s3tools-demo/some/path/

需要注意的是,s3cmd sync 首先检查 目的 已存在的文件的列表和详细信息,与==本地文件进行比较==,然后仅上传远程不存在或具有不同大小或 md5 校验和的文件。如果您运行了上述所有示例,您将从同步中获得与以下输出类似的输出:

$ s3cmd sync  ./  s3://s3tools-demo/some/path/
dir2/file2-1.log -> s3://s3tools-demo/some/path/dir2/file2-1.log  [1 of 2]
dir2/file2-2.txt -> s3://s3tools-demo/some/path/dir2/file2-2.txt  [2 of 2]

Reference

[1] s3cmd Files

本文发布于Cylon的收藏册,转载请著名原文链接~

链接:https://www.oomkill.com/2023/09/05-3-s3cmd/

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」 许可协议进行许可。