Yandex Object Storage – implementation of SignedURL for loading data into a bucket on NodeJS

A few years later, I again faced the same task, but the customer insisted on domestic cloud storage.

Below I will tell you a little about what signed links are, how to switch to Yandex Object Storage (YOS), and why YOS and AWS S3 are one and the same!


Client/server optimization when working with binary files

Fig 1 - Options for uploading files to the cloud with a client-server architecture
Fig 1 – Options for uploading files to the cloud with a client-server architecture

What are signed links?

This is a URL containing data for request authorization in its parameters.

That is, for sending any binary file to a specific link, it will be automatically uploaded to the cloud after validating the parameters from the url.

What is the advantage?

In the case of the classical architecture, the user first uploads the file to the client, then the client sends a request with the file in the body to the server, and only after that the server sends the same file to the cloud.

It turns out that we load one file from one structure element to another 3 times.
Of course, if the file is small in size, then there will be no downtime, and the server will not be heavily loaded.
But what if it’s a long video, in good resolution???

In the case of signed links, the file is downloaded directly from the client to the cloud, bypassing the server.

Accordingly, it turns out that the download takes place only 2 times from the user to the client, and from the client to the cloud. We reduce the operating time of 1 operation by + – 1.5 times.


Yandex Object Storage

I started working on the task itself from registering on Yandex Cloud, the glory is that the concept is the same as that of AWS.

Fig 2 - Cloud creation interface on YOS
Fig 2 – Cloud creation interface on YOS

Important: After creating a service account, don’t forget to add a static access key to it and save them!

Fig 3 - Service account creation interface
Fig 3 – Service account creation interface

On this, consider that the work with the site is over, then there will be code, code and code again.


AWS SDK and Yandex Object Storage

Thinking about the technical implementation, I figured that the essence of the signed url from the storage location does not change. And after all, I was right, having looked in the documentation of AWS S3 and YOS how the link is generated, I did not notice any differences.

Here it was decided to use the already familiar SDK for NodeJS from Amazon.

import AWS from 'aws-sdk';
import { AWS_REGION, AWS_USER_KEY, AWS_USER_SECRET_KEY } from '../../config';

const accessKeyId = AWS_USER_KEY;
const secretAccessKey = AWS_USER_SECRET_KEY;

AWS.config.update({ region: AWS_REGION, accessKeyId, secretAccessKey });

export default AWS;
const client = new aws.S3({
  endpoint: 'https://storage.yandexcloud.net',
});
export const getSignedUrl = async ({ type }: GetSignedUrlInput): Promise<GetSignedUrlResponse> => {
  const action = 'putObject';
  let objectKey = cuid();

  let params = {
    Bucket: AWS_BUCKET_NAME,
    Key: `${objectKey}.${mime.extension(type)}`,
    ContentType: type,
    Expires: Number(SIGN_URL_EXPIRES),
    ACL: 'public-read',
  };

  const signedURL: string = await new Promise((resolve, reject) => {
    client.getSignedUrl(action, params, (err, url) => {
      if (err) {
        reject(err);
      } else {
        resolve(url);
      }
    });
  });

  return { signedURL, objectURL: `https://${AWS_BUCKET_NAME}.storage.yandexcloud.net/${params.Key}`, expensive: params.Expires };
};

Now let me explain what is happening here:

  1. We create a function that we will call later to get a link, what to pass into it – your decision, I only had the type of file to upload. You need to return at least a signed link

  2. Next, I create an object with the parameters by which the link will be built, only Bucket and Key are important, the rest of the fields are optional

  3. Based on the parameters object, we generate a link using a function from the AWS SDK.

  4. PROFIT!!! All is ready)

Now for the client side.

The link contains all the necessary headers for successful authentication.

It is worth noting that the request type when sending to YOS must be PUT, I did not find information about this in the documentation, but AWS S3 has a POST request type.


Conclusion

This is my first article, I didn’t even think of writing it in the morning, and this came out in the evening.

Why did I write this? Just to help those who face such a task, because there is little information on using YOS on NodeJS, and I did not find one that would help me.

Thanks to everyone who has read so far!

Similar Posts

Leave a Reply