Coder Social home page Coder Social logo

Comments (2)

cwomack avatar cwomack commented on June 3, 2024 1

Hey, @natuan62 👋. We appreciate you opening this issue and I'm going to mark it as a feature request for v6 of Amplify for version parity from v5. Previously, you could use something like the file type to create those dynamic prefixes in S3 upon uploading.

For v6 (and using Gen 1 with CLI created backend resources), it looks like we may need to do something along the lines of exposing extra arguments for a custom prefixResolver(), such as file type, and make this optional. However, in the Gen 2 Developer Experience (which is currently in developer preview) this should be supported once we go General Availability. The functionality isn't released yet, but we will follow up on this issue as soon as it is.

As for the code snippet for a a solution you provided above, there's a risk of causing concurrent calls to S3 calls in your promise callback, which could lead to potential race conditions. If the uploaded file is larger than 5 MB, it will call the multi-part upload and make multiple calls within the promise. I'm concerned that you may end up with different calls leading to different prefixes if this happens.

Best recommendation here would be to await the release of this functionality in the Gen2 Developer Experience, or until the feature request is fully supported on the Gen 1 side of things.

from amplify-js.

natuan62 avatar natuan62 commented on June 3, 2024

I found this solution, seem it not good. Anyway better this way @cwomack ?

const uploadS3 = async (input: {file: string, key: string}, accessLevel: AccessLevel) => {
    const libraryOptions: any = {
      Storage: {
        S3: {
          prefixResolver: async (input: { accessLevel: AccessLevel, targetIdentityId: string}) => {
            const { targetIdentityId } = input;
           
            const prefix = input.accessLevel === 'private' ? 'video' : 'image';
            return `private/${prefix}/${targetIdentityId}/`;
          },
        },
      },
    };
    Amplify.configure(awsconfig as ResourcesConfig, libraryOptions);

    const operation = uploadData({
      key: input.key,
      data: input.file,
      options: {
        contentType: 'text/plain',
        accessLevel,
      },
    });
    const result = await operation.result;
  };

  const listFiles = [{file: 'video1', type: 'video'}, {file: 'image1', type: 'image'}];

  await Promise.all(listFiles.map((file, index) => {
    const accessLevel = file.type === 'video' ? 'private' : 'protected';
    return uploadS3({file: file.file, key: file.file}, accessLevel);
  }

from amplify-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.