一些S3cmd命令

下载文件到本地:(注意最后有一个点代表当前文件夹)

s3cmd sync s3://wikipediatraf/201306-gz/pagecounts-20130601-000000.gz .


查看文件夹

 s3cmd ls s3://bucketname



How to Install s3cmd Package

s3cmd is available in default rpm repositories for CentOS,RHEL and Ubuntu systems, You can install it using simply executing following commands on your system.

CentOS/RHEL Users:
# yum install s3cmd
Ubuntu Users:
$ sudo apt-get install s3cmd
Configure s3cmd Environment

In order to configure s3cmd we would required Access Key and Secret Keyof your S3 Amazon account. Get these security keys from aws securityCredentials page. If will prompt to login to your amazon account.

After getting key files, use below command to configure s3cmd.

# 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
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'
Uses of s3cmd Command Line

Once configuration is successfully completed. Now find below command details to how to manage s3 buckets using commands.

1. Creating New Bucket

To create a new bucket in Amazon s3 use below command. It will create bucket named tecadmin in S3 account.

# s3cmd mb s3://tecadmin

Bucket 's3://tecadmin/' created
2. Uploading file in Bucket

Below command will upload file file.txt to s3 bucket using s3cmd command.

# s3cmd put file.txt s3://tecadmin/

file.txt -> s3://tecadmin/file.txt  [1 of 1]
 190216 of 190216   100% in    0s  1668.35 kB/s  done
3. Uploading Directory in Bucket

If we need to upload entire directory use -r to upload it recursively like below.

# s3cmd put -r backup s3://tecadmin/

backup/file1.txt -> s3://tecadmin/backup/file1.txt  [1 of 2]
 9984 of 9984   100% in    0s    18.78 kB/s  done
backup/file2.txt -> s3://tecadmin/backup/file2.txt  [2 of 2]
 0 of 0     0% in    0s     0.00 B/s  done

Make sure you are not adding trailing slash in upload directory named backup (eg: backup/), else it will upload only content of backup directory only.

# s3cmd put -r backup/ s3://tecadmin/

backup/file1.txt -> s3://tecadmin/file1.txt  [1 of 2]
 9984 of 9984   100% in    0s    21.78 kB/s  done
backup/file2.txt -> s3://tecadmin/file2.txt  [2 of 2]
 0 of 0     0% in    0s     0.00 B/s  done
4. List Data of S3 Bucket

List the objects of s3 bucket using ls switch with s3cmd.

# s3cmd ls s3://tecadmin/

                       DIR   s3://tecadmin/backup/
2013-09-03 10:58    190216   s3://tecadmin/file.txt
5. Download Files from Bucket

Some times if we need to download files from s3 bucket, Use following commands to download it.

# s3cmd get s3://tecadmin/file.txt

s3://tecadmin/file.txt -> ./file.txt  [1 of 1]
 4 of 4   100% in    0s    10.84 B/s  done
6. Remove Data of S3 Bucket

To remove files are folder from s3 bucket use following commands.

 Removing file from s3 bucket 
# s3cmd del s3://tecadmin/file.txt

File s3://tecadmin/file.txt deleted

 Removing directory from s3 bucket 
# s3cmd del s3://tecadmin/backup

File s3://tecadmin/backup deleted
7. Remove S3 Bucket

If we don’t need s3 bucket anymore, we can simply delete it using following command. Before removing bucket make sure its empty.

# s3cmd rb s3://tecadmin

ERROR: S3 error: 409 (BucketNotEmpty): The bucket you tried to delete is not empty

Above command failed because of s3 bucket was not empty

To remove bucket first remove all objects inside bucket and then use command again.

# s3cmd rb s3://tecadmin

Bucket 's3://tecadmin/' removed
8. List All S3 Bucket

Use following command to list all s3 buckets in your account

# s3cmd ls






Setting Up s3cmd  tool on Windows

1.      Download s3cmd  from s3tools –
http://s3tools.org/download
2.      Download python 2.7 for windows –
                 http://www.python.org/getit/

Set Python path
                Go to Environment Variables à
                Edit path variable and append  python path - ‘C:\Python27’ to it.
Go to path of s3cmd  file
·          Install

   python setup.py install

·          Configure

   python s3cmd  -- configure
  
  It will ask for 
  
  1)Access Key
  2)SecretKey
  3)Encryption password for python s3cmd  tool – It will used to encrypt the file while being         uploaded and will decrypt with same password while being downloaded.
4)Path to GPG program – No.
5)Use HTTPS protocol [Yes/No]
6)HTTP Proxy Server Name
7) HTTP Proxy Server Port

Save this settings.

·          Run

Example – Command to list all buckets

              python s3cmd  ls

Commands:
                                I.            Make bucket
python s3cmd  mb s3://BUCKET
                              II.            Remove bucket
python s3cmd  rb s3://BUCKET
                            III.            List objects or buckets
python s3cmd  ls [s3://BUCKET[/PREFIX]]
                            IV.            List all object in all buckets
python s3cmd  la
                              V.            Put file into bucket
python s3cmd  put FILE [FILE...] s3://BUCKET[/PREFIX]
                            VI.            Get file from bucket
python s3cmd  get s3://BUCKET/OBJECT LOCAL_FILE
                          VII.            Delete file from bucket
python s3cmd  del s3://BUCKET/OBJECT
                        VIII.            Synchronize a directory tree to S3
python s3cmd  sync LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR
                            IX.            Disk usage by buckets
python s3cmd  du [s3://BUCKET[/PREFIX]]
                              X.            Get various information about Buckets or Files
python s3cmd  info s3://BUCKET[/OBJECT]
                            XI.            Copy object
python s3cmd  cp s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
                          XII.            Move object
python s3cmd  mv s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
                        XIII.            Modify Access control list for Bucket or Files
python s3cmd  setacl s3://BUCKET[/OBJECT]
                        XIV.            Set an access policy for a bucket
python s3cmd  setpolicy s3://BUCKET POLICY_STRING
                          XV.            Enable/disable bucket access logging
python s3cmd  accesslog s3://BUCKET
                        XVI.            Sign arbitrary string using the secret key
python s3cmd  sign STRING-TO-SIGN
                      XVII.            Sign an S3 URL to provide limited public access with expiry
python s3cmd  signurl s3://BUCKET/OBJECT expiry_epoch
                    XVIII.            Fix invalid file names in a bucket
python s3cmd  fixbucket s3://BUCKET[/PREFIX]
                        XIX.            Create Website from bucket
python s3cmd  ws-create s3://BUCKET
                          XX.            Delete Website
python s3cmd  ws-delete s3://BUCKET
                        XXI.            Info about Website
python s3cmd  ws-info s3://BUCKET
                      XXII.            List CloudFront distribution points
python s3cmd  cflist
                    XXIII.            Display CloudFront distribution point parameters
python s3cmd  cfinfo [cf://DIST_ID]
                    XXIV.            Create CloudFront distribution point
python s3cmd  cfcreate s3://BUCKET
                      XXV.            Delete CloudFront distribution point
python s3cmd  cfdelete cf://DIST_ID
                    XXVI.            Change CloudFront distribution point parameters
python s3cmd  cfmodify cf://DIST_ID
                  XXVII.            Display CloudFront invalidation request(s) status
python s3cmd  cfinvalinfo cf://DIST_ID[/INVAL_ID]








你可能感兴趣的:(一些S3cmd命令)