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

Feat/support binary and stream uploads #29

Merged
merged 5 commits into from
Jun 16, 2021

Conversation

inian
Copy link
Member

@inian inian commented Jun 15, 2021

createObject supports both FormData via a multipart/form-data content-type and a binary upload via the POST body.

If using the binary upload, the mimeType and cacheControl should be sent via the content-type and cache-control headers (This is where a multipart upload is cleaner but since formdata is not supported in Node, had to implement this)

Backend support for supabase/storage-js#5

From Node, you can do

const MY_FILE_PATH = "image-stream.jpg";
const fs = require("fs");
const axios = require("axios");

const readmeStream = fs.createReadStream(MY_FILE_PATH);
readmeStream.on("error", console.log);
const { size } = fs.statSync(MY_FILE_PATH);

axios({
  method: "POST",
  url: `http://localhost:5000/object/bucket2/${MY_FILE_PATH}`,
  headers: {
    "Content-Type": "image/jpeg",
    "Content-Length": size,
    Authorization:
      "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...,
  },
  data: readmeStream,
});

Or directly send a buffer

const MY_FILE_PATH = "image-stream.jpg";
const fs = require("fs");
const axios = require("axios");

const { size } = fs.statSync(MY_FILE_PATH);

axios({
  method: "POST",
  url: `http://localhost:5000/object/bucket2/${MY_FILE_PATH}`,
  headers: {
    "Content-Type": "image/jpeg",
    "Content-Length": size,
    Authorization:
      "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  },
  data: fs.readFileSync(MY_FILE_PATH),
});

From Browser you can do the same thing via a File Object without using FormData

@danalloway
Copy link

this is excellent, and SO helpful @inian thank you for working on this!

@inian inian merged commit c42ede3 into master Jun 16, 2021
@inian inian deleted the feat/support-binary-and-stream-uploads branch June 16, 2021 06:03
@github-actions
Copy link

🎉 This PR is included in version 0.5.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants