In this guide, we will learn how to use s3cmd as a client for ArvanCloud. S3cmd is a tool that allows you to create, list, or delete buckets from the terminal and download, upload, and delete objects to/from your bucket.
Installing S3cmd
You can install S3cmd from the command line using your Linux distribution’s Package Manager.
Enter the following command to update and upgrade your packages:
apt update && apt upgrade -y
Enter the following command to install s3cmd:
apt install s3cmd
S3cmd Configuration
Create a configuration file called .s3cfg in the HOME directory:
cd $HOME touch .s3cfg
Open the file with a text editor like nano:
nano .s3cfg
Copy and paste the following text into the file, then save and close it. Note that you must replace the Access Key and Secret Key values with your own, which are available in the dashboard of your user panel.
[default] # Login credentials access_key = <ACCESS_KEY> secret_key = <SECRET_KEY> host_base = https://s3.ir-thr-at1.arvanstorage.ir host_bucket = https://s3.ir-thr-at1.arvanstorage.ir use_https = True
An alternative solution to creating a configuration file is to use the s3cmd –configure command.
Using Object Storage with S3cmd
The features available in the Object Storage user panel or tools such as S3browser can be easily implemented with the help of S3cmd. In the following, we will examine the commands related to working with ArvanCloud Object Storage using S3cmd.
Creating a New Bucket
Enter the following command to create a new bucket. Be careful that the bucket’s name must be a unique combination of numbers and small letters. Here we have chosen the name “newbucket”.
s3cmd mb s3://newbucket
The output of the command will be similar to the following:
Bucket 's3://newbucket/' created
Uploading Files to a Bucket
You can use the following command to upload the file to the bucket:
s3cmd put logs.txt photo1.jpg s3://newbucket
The output of this command will be as follows:
logs.txt -> s3://newbucket/logs.txt [1 of 2] 0 of 0 0% in 0s 0.00 B/s done photo1.jpg -> s3://newbucket/photo1.jpg [2 of 2] 0 of 0 0% in 0s 0.00 B/s done
Listing Files in a Bucket
To view the list of files in a bucket, use the following command. Here we list all the files in the “newbucket” bucket:
s3cmd ls s3://newbucket
You should see an output similar to the following, which includes the files inside the bucket:
2021-07-10 06:23 0 s3://newbucket/logs.txt 2021-07-10 06:23 0 s3://newbucket/photo1.jpg
Downloading a File from the Bucket
Use the following command to download an object from the bucket to your system. For example, here we downloaded the logs.txt file from “newbucket”:
s3cmd get s3://newbucket/logs.txt logs.txt
The output will be as follows:
s3://newbucket/logs.txt -> logs.txt [1 of 1] 0 of 0 0% in 0s 0.00 B/s done
Removing a File from the Bucket
You can use the following command to delete a file from the bucket. For example, here we delete the logs.txt file from the “newbucket”:
s3cmd del s3://newbucket/logs.txt
You should see the output as follows:
delete: 's3://newbucket/logs.txt'
Deleting a Bucket
To delete a bucket, use the following command. Here we delete “newbucket”:
s3cmd rb s3://newbucket
The output will be similar to the following:
Bucket 's3://newbucket/' removed
CORS Configuration
The CORS standard allows browsers to request resources from other domains if they have permission. This feature helps you configure how your buckets are accessed from browser-based apps and websites.
Setting the CORS Configuration on a Bucket
Create a CORS configuration file and open it with a text editor:
nano cors.xml
Put your settings and configuration in the file, save and close it:
<CORSConfiguration> <CORSRule> <AllowedOrigin>http://www.example.com</AllowedOrigin> <AllowedMethod>PUT</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>DELETE</AllowedMethod> <AllowedHeader>*</AllowedHeader> <MaxAgeSeconds>3000</MaxAgeSeconds> <ExposeHeader>x-server-side-encryption</ExposeHeader> </CORSRule> </CORSConfiguration>
Use the following command and replace your bucket name with “bucketname” to set this information on it.
s3cmd setcors cors.xml s3://bucketname
Removing CORS Settings from a Bucket
Run the following command to remove the CORS configuration of a bucket. Replace your bucket name with “bucketname”:
s3cmd delcors s3://bucketname
Other Commands
For more information about the various s3cmd commands, refer to the official documentation by running the “s3cmd –help” command.