question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi i would like to be able to upload images that is in base64 format. There is a bug in another library I’m using so i cannot get the image, but I can get the base64 representation of the image. So i would like to upload that to S3 and have it become a actual image. Any ideas on how to do this ?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
frayeralexcommented, Sep 11, 2017

My solution with reading file, decode to ArrayBuffer and upload to s3

// @flow
import { S3 } from "aws-sdk";
import { config } from "../environments/config";
import fs from "react-native-fs";
import { decode } from "base64-arraybuffer";

// init s3 client with your credentials
const s3 = new AWS.S3();

// path = "file://...", name = "profile.jpg"
const uploadFile = async (path: string, name: string): Promise => {
    try {
      const base64 = await fs.readFile(path, "base64");
      const arrayBuffer = decode(base64);
      const params = {
        Body: arrayBuffer,
        Bucket: config.aws.s3.bucket,
        Key: `${config.aws.s3.folder}/${name}`,
        ACL: "public-read",
        ContentEncoding: "utf-8",
        ContentType: "binary"
      };
      const uploadData = await s3.putObject(params).promise();
      return Promise.resolve(uploadData);
    } catch (error) {
      return Promise.reject(error);
    }
}
3reactions
MrHubblecommented, Jul 26, 2017

I ended up with a working solution by using aws-sdk instead:

const Buffer = global.Buffer || require('buffer').Buffer;
var AWS = require('aws-sdk/dist/aws-sdk-react-native');
AWS.config.update({
  accessKeyId: myKeyId,
  secretAccessKey: mySecretKey,
  region: myRegion
})

var s3 = new AWS.S3()
buf = new Buffer(base64image.replace(/^data:image\/\w+;base64,/, ""), 'base64')
let keyPrefix = `uploads/image.png`    
var params = {
  Bucket: "mybucket",
  Key: keyPrefix,
  Body: buf,
  ContentType: 'image/png',
  ContentEncoding: 'base64',
  ACL: 'public-read'  
}
let putObjectPromise = await s3.upload(params).promise()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to upload files from your HTML form using Base64 ...
Uploading files using Base64 encoding is a common practice, ... this guide I'm going to show you how to upload files using base64...
Read more >
How to upload files from your HTML form using Base64 ...
Uploading files using Base64 encoding is a common practice. In this guide, I'm going to show you how to upload files using base64...
Read more >
Base64 encoder, file upload, an easy to use online tool
A Base64 (RFC 4648) encoder. Encodes an uploaded binary or text file (a text or a binary data like a sound, an image...
Read more >
Upload base64 image with Ajax - Stack Overflow
So I'm wondering what is the proper way to send a base64 image to the server using an Ajax request WITHOUT using a...
Read more >
Base64 Image Encoder
Optimize your images and convert them to base64 online. Drag & Drop your files, ... You can upload up to 20 images (max....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found