Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React] Add S3 signed url image upload example #118

Open
pabloferro opened this issue Jan 21, 2021 · 0 comments
Open

[React] Add S3 signed url image upload example #118

pabloferro opened this issue Jan 21, 2021 · 0 comments
Labels
recipe New recipe / feature for recipes

Comments

@pabloferro
Copy link
Contributor

pabloferro commented Jan 21, 2021

Summary

Add S3 signed url image upload example

Useful info

Creating an apisauce instance and uploading blob to S3

signedURL should come from an api call to the API.

import { create } from 'apisauce';
const uploadApi = create({ baseURL: '' });

const uploadFile = ({ signedURL, name, data, type, }) =>
    const headers = {
      'Content-Type': type
    };
    return uploadApi.put(signedURL, data, { headers }).then(uploadResponse => {
      if (!uploadResponse.ok) {
        throw Error('errors.fileUpload');
      }
      return response.data.url.split('?')[0];
    });
  });

Uploading from file

const uploadFromFile = (file, resource) =>
  uploadFile({ name: file.rawFile.name, data: file.rawFile, type: file.rawFile.type, resource });

Uploading from base64

const uploadImageFromBase64 = (src, resource, index) => {
  const blob = dataURItoBlob(src);
  return uploadFile({
    name: `content_image_${index}.${blob.type.split('/')[1]}`,
    data: blob,
    type: blob.type,
    resource
  });
};
@pabloferro pabloferro added the recipe New recipe / feature for recipes label Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe New recipe / feature for recipes
Projects
None yet
Development

No branches or pull requests

1 participant