AWS services moving forward - 3rd party services depreciation

Hi @Jason_Tulloch and @henna.s,

I just wanted to chime in here as we have now rolled out the transition to production and no one has noticed any performance changes which is great for me! As when I started this thread I was quite worried.

@Jason_Tulloch I was also running into the same speed issue with uploading images directly to the serverless function by converting it to binary from memory and then getting the 3rd party services to upload and it worked well. However, when I changed to s3.putObject() from the AWS SDK it was really slow.

Therefore I now just upload the images/files by creating a S3 presigned URL with a realm function which is super quick, and then I use axios to do a PUT request of the file to that presigned URL. I have put an example below for you.

Realm function

exports = async function (args) {
  const bucket = "my-s3-bucket";

  const S3 = require("aws-sdk/clients/s3");
  const s3 = new S3({
    accessKeyId: context.values.get("awsAccessKeyId"),
    secretAccessKey: context.values.get("awsSecretAccessKey"),
    region: "ap-southeast-2"
  });

  const presignedUrl = await s3.getSignedUrlPromise("putObject", {
    Bucket: bucket,
    Key: args.Key,
    ContentType: args.ContentType,
    // Duration of the lifetime of the signed url, in milliseconds
    Expires: 900000
  });
  return presignedUrl;
};

Frontend JS code

import { realmUser } from "../../main";
const axios = require("axios");

export default class UploadFile {
  async handleFileUpload(file) {
    const returnObj = {
      s3Path: "",
      s3: {},
    };
    if (file.size > 26214400) {
      alert("No files over 25 MB supported");
      return false;
    }

    const key = `files/${file.name}`;
    returnObj.s3Path = key;
    // AWS S3 Request
    const args = {
      ContentType: file.type,
      Key: key,
    };

    try {
      const presignedUrl = await this.getPresignedS3URL(args);
      const options = {
        headers: {
          "Content-Type": file.type,
        },
      };
      // Saves the file to S3
      await axios.put(presignedUrl, file, options);
      returnObj.s3.key = key;
    } catch (error) {
      console.log(error);
    }

    // Return the data back to a componenet
    return returnObj;
  }

  async getPresignedS3URL(args) {
    return new Promise((resolve, reject) => {
      realmUser.functions
        .uploadTestBuyFile(args)
        .then((doc) => {
          resolve(doc);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}

Hope this helps you!

Cheers

2 Likes