How to upload any file on Amazon S3 using Rust?

Table of contents
Reading Time: 3 minutes

Welcome everyone to the file upload on Amazon S3 using the Rust.

Amazon S3 [Amazon Simple Storage Service] provides virtually limitless storage on the internet. For the bucket creations and security credentials please refer to my last blog. This blog explains following requests using Rust:

  • sending a request to aws S3 bucket,
  • list of objects in the bucket,
  • putting  an object in the bucket,
  • deleting an object from the bucket, and
  • getting an object from the bucket.

Let’s take a simple example of an image file:

screenshot from 2019-01-12 18-48-53
 Fig.1: This consists all the imports required

In Fig.1, line no. 10 “pub mod constants” is used to create a new module named by constants to externalize all the paths and magic number and line no.12,14,15 for importing all the constants.

Fig.2: Externalized all the paths and the magic number

In Fig.2, we have created several constants for bucket’s name [line no.1], region’s name [line no.2], the path of a local file to be uploaded [line no.3], the path of a bucket where local file is to be upload [line no.6], etc in module/file.

screenshot from 2019-01-12 19-07-26
Fig.3: Setting credentials and instantiating a new bucket

We can get credentials in three ways:


AWS access credentials: access key, secret key, and an optional token.

1. Loads credentials directly


2. Loads credentials from the environment


3. Loads credentials from the profile

    3.1 Loads credentials from [default] profile

let credentials: Credentials = Credentials::new(None, None, None, None); 


let credentials: Credentials = Credentials::default();

3.2 Loads credentials from [“my-profile”] profile

let credentials: Credentials = Credentials::new(None, None, None, Some("my-profile".into()));

screenshot from 2019-01-12 20-01-17
Fig.7: Snippet of code

In Fig.7, you can see that Bucket::new() is used to instantiate a new Bucket. bucket.list lists all the contents of an S3 bucket and bucket.delete deletes file from an S3 path. All requests on S3 bucket returns a tuple (data, code) where the code is of u32 type, represents the status code for that particular request. 

assert_eq!(STATUS_CODE, code); tests the particular request by matching left value to the right value.

Fig.8: Code Snippet

In Fig.8, File::open(..) is used to open a file in reading mode only then read_to_end() reads the content of a file to the end and stored in a vector content .

put() method is used to put the content in the bucket and get() method is used to retrieve the file from S3 bucket.

If you will merge the code of Fig.1, 2, 3, 7, 8, you will get full code for all types of request to S3 bucket. 

Output by adding the dependency rust-s3 = “0.11.0” in cargo.toml file and executing the code cargo run :


And you can now check your S3 bucket, it will have the file which you have put in it. This code can work for any type of file extensions.

I hope this blog helps you in some way, comment in the inbox  for any type of query.

Written by 

Amita Yadav is a Software Consultant Trainee at Knoldus Software INC, currently working on RUST, system programming language. She is self-determined at what she has to do. She likes to learn trending technologies. She is familiar with languages such as C++, C, Java, Scala and technologies like lagom, Play, Akka, Kafka, spark, and databases like Cassandra, MySql, Sqlite, Slick. She loves to create amazing videos mashup.

2 thoughts on “How to upload any file on Amazon S3 using Rust?4 min read

Comments are closed.