用 s3cmd 挂载云储存服务器

s3cmd 是亚马逊 s3 的一个命令行工具,稍作修改可以用在国内的云存储。服务器 Centos,如果有 epel 源 的话,可以直接

yum install s3cmd

或者直接去 SF 下载,传送 s3cmd Projects
解压软件包,执行 ./s3cmd --configure,如果没有 python-dateutil,就会报以下错误。

1
2
3
4
5
6
7
8
9
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ImportError trying to import dateutil.parser.
Please install the python dateutil module:
$ sudo apt-get install python-dateutil
  or
$ sudo yum install python-dateutil
  or
$ pip install python-dateutil
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

我系统 centos,这时候执行 yum install python-dateutil,然后在测试。
yum install 安装此软件的,然后:

1
s3cmd --configure

用源码包安装的,需要去软件解压目录执行, 类似 windows 下的免安装绿色软件。

1
./s3cmd --configure
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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.
Access Key: 这里输入 AK
Secret Key: 这里输入 SK
Default Region [US]: CN  #默认美国市区,可更改

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: 	# 我理解为一种链接对话加密
Path to GPG program [/usr/bin/gpg]: 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 only be proxied with Python 2.7 or newer
Use HTTPS protocol [No]:  是否使用 https,默认 NO

On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't connect to S3 directly
HTTP Proxy server name:  是否使用代理,默认空

New settings:
  Access Key: 显示你的 AK
  Secret Key: 显示你的 SK
  Default Region: CN
  Encryption password:
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: False
  HTTP Proxy server name:
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] n	# 是否测试这个链接

Save settings? [y/N] y		# 是否保存文件
Configuration saved to '/root/.s3cfg'		# 保存文件地址

按照提示,填入密匙,其它选项一路默认回车即可,后面问是不是要测试连接的时候选 No(因为 s3cmd 在修改前默认连接是亚马逊 s3),
然后保存,程序会在用户跟目录下生成一个隐藏文件,我们要修改这个配置文件:

1
vim ~/.s3cfg

找到 “host_base =” 和 “host_bucket =” 这两行,修改至如下:

1
2
host_base = s3.domain.com
host_bucket = %(bucket)s.s3.domain.com

好了,现在试试 s3cmd 的一个常用命令:

1
s3cmd ls

如果可以看见你在对象云存储里建立的容器,那表示配置成功,另外一些常用的命令如下,用 s3cmd –help 可以查看

动作命令
创建容器s3cmd mb s3://BUCKET
删除容器s3cmd rb s3://BUCKET
查看容器内的文件s3cmd ls s3://BUCKET/path/
查看所有容器内的所有文件,我测试过只能显示一层目录s3cmd la
把文件从本地上传至云存储的命令s3cmd put FILE [FILE…] s3://BUCKET[/PREFIX]
从云存储下载文件到本地s3cmd get s3://BUCKET/OBJECT LOCAL_FILE
删除容器内的特定文件s3cmd del s3://BUCKET/OBJECT
如果要删除容器类的整个目录, 可以加参数 -r -fs3cmd del -r -f s3:// 容器 / 目录 /
查看容器所占空间s3cmd du [s3://BUCKET[/PREFIX]]
查看容器或者文件的属性s3cmd info s3://BUCKET[/OBJECT]
云存储中的文件复制命令s3cmd cp s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
移动文件s3cmd mv s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
把本地目录同步到云存储或者从云存储同步到本地s3cmd sync LOCAL_DIR s3://BUCKET[/PREFIX] \or s3://BUCKET[/PREFIX] LOCAL_DIR

常用的命令如下,复杂一点的可以去啃 s3cmd 官方文档。

  • 同步本地至云存储,并在云端删除已经从本地删除的文件

    s3cmd sync --delete-removed /path/ s3://bucket/path/

  • 如果怕错删除,可以再加参数 –dry-run,它只列出 –delet-removed 将要删除的文件,但并不会真正的删除

    s3cmd sync --dry-run --delete-removed /path/ s3://bucket/path/

  • sync 命令默认是要校检本地文件和云存储中文件的 md5 值的,如果不想校检只同步新文件,可以加 –skip-existing

    s3cmd sync --skip-existing /path/ s3://bucket/path/

  • 还有 –exclude(不包含) 和 –include(包含)参数

    s3cmd sync --exclude '*.txt' --include 'dir2/*' . s3://bucket/path/

  • 云存储可以用来存储网站上的静态内容的:.css、.js、.jpg 一类。这类文件加入 Cache-Control header 可以减少请求数量和流量,参数是 –add-header,命令如下:

    s3cmd put --add-header='Cache-Control:max-age=31536000' -M -r jpg_folder s3://domain.com/

  • 如果想进一步降低流量消耗,可以考虑 gzip 压缩 css 和 js 文件,毕竟流量在云存储里是算钱的,但貌似云存储服务器端并不支持 gzip 压缩,
    所以我们要预先压缩文件。我的做法是先 “gzip -9” 压缩 css 文件得到 *.css.gz ,
    再批量重命名 *.css.gz 为 *.css,最后上传 *.css 文件。这时需要给已经压缩过的文件加 Content-encoding header,
    否则浏览器读不出来,命令如下:

    s3cmd put --add-header='Cache-Control:max-age=31536000' --add-header='Content-encoding:gzip' -M *.css s3://domain.com/css/