AWS S3 is a widely used cloud-based storage service. You can directly attach your public and private S3 buckets to ImageKit. Once that is done, you would be able to access all the images in your S3 bucket via ImageKit using image URLs. Each image would get optimized and resized as per the default ImageKit settings and URL transformation parameters.

Here are the steps that you need to follow to attach your S3 bucket to ImageKit and start delivering optimized images from that bucket instantly.

Steps for optimizing images in an AWS S3 Bucket with ImageKit

1. ImageKit needs read-only access to your S3 bucket. You can achieve this by using AWS Identity and Access Management (AIM) policy for your S3 bucket.

Ideally, you should create a separate user for this purpose and assign the IAM policy for that particular user and the required buckets only. Note that you need to provide permission for the operation s3::GetObject on the objects in your bucket. The resource value in the IAM policy would look like this arn:aws:s3:::test/* (test being the bucket name)

You can reference these articles to learn more about granting read-only access to your S3 bucket using AWS IAM policy.

Writing IAM Policies: How to Grant Access to an Amazon S3 Bucket

Examples - Managing access on S3 Bucket

2. Once access has been granted using IAM policies, then note down the AWS Access Key and AWS Secret Key for this. We will use this in the ImageKit dashboard to attach the bucket.

3. Log in to your ImageKit dashboard. Go to the Integration Section.

4. AWS S3 Bucket is supported as an origin in ImageKit. Click on "Add origin" button

5. In the form that opens up, select “Amazon S3” as the “Origin Type”. You can give this source any “Origin Name”. Origin Name is to help you identify this origin and has no impact on access or performance. Below, we are using the name "Test Bucket" and using some sample values to demonstrate adding an S3 bucket as origin.

6. Provide the bucket name in the “S3 Bucket Name” field. The images in your S3 bucket could have a fixed prefix (i.e. they appear inside a particular folder). If that is the case, then you can specify the full prefix to your images in the “S3 Bucket Folder” field. If your images are present at the base or root of your S3 bucket then you can enter “/” here.

7. Use the Access Key and the Secret Key for the user you created in step 1 in the “S3 Access Key” and “S3 Secret Key” fields respectively.

8. Click on “Submit".

(You can refer to the use of the field "Include Canonical Response Header" here[link])

9. In the “Image URL Patterns” section, you will find this new source added in “Image Origin preference” list with the “Default URL pattern”. In the screenshot below, the origin Test Bucket now appears in the origin preference list.

10. You can either continue to use the “Default URL pattern” or create a new pattern for your S3 images as described here[link].

Note

ImageKit does not download objects from the S3 storage to optimize images. Whenever you request an image in your S3 bucket, using an ImageKit URL and when the image is not available in ImageKit’s storage, only then will it get the image from the S3 bucket. Image optimization and transformation is done in real time. 

Example URL Construction for AWS S3 bucket images

If the path of the image in your S3 bucket is “/path/to/my/image.jpg”, then the image will be accessible on this path

Image Key (or Path) in S3 Bucket

/path/to/my/image.jpg

With default URL pattern

https://ik.imagekit.io/{imagekitId}/path/to/my/image.jpg

With custom URL pattern

https://ik.imagekit.io/{imagekitId}/{pattern}/path/to/my/image.jpg

With a custom domain name (default pattern or mapped to a particular pattern)

https://images.example.com/path/to/my/image.jpg

We are always there to help you in case of any doubt. You can create a support ticket from your ImageKit dashboard and our team will help you with the integration.

Did this answer your question?