Media Uploader
This is for when you want to upload some media to s3 but also tell your API some metadata about that media. You say "Hey API! Got a file for ya! Also, here's some metadata!" the api says "Cool! I'll keep the metadata. Here's a signature for sending the file straight over to s3!" Then, you send the file to s3. Great!
Install
First, install nodejs.
Then, from the command line,
npm install --global @amonks/upload-media
Configure
You can configure Media Uploader using environment variables.
These environment variables are required:
- UPLOAD_MEDIA_API_KEY
- UPLOAD_MEDIA_ENDPOINT_URL
You can also pass metadata to the server by using additional environment variables that begin with UPLOAD_MEDIA_METADATA_
.
Use
From the command line
Once you've installed this module, you can use it from the command line. This works on Windows.
upload-media my-picture.jpg
One way of setting the required environment variables is like this:
UPLOAD_MEDIA_API_KEY="abc123" UPLOAD_MEDIA_ENDPOINT_URL="https://whatever.com/upload" upload-media my-picture.jpg
Another way is to make a file called .env
. That file has to be in whatever folder you call upload-media
from. It should look like this:
UPLOAD_MEDIA_API_KEY="abc123"
UPLOAD_MEDIA_ENDPOINT_URL="https://whatever.com/upload"
UPLOAD_MEDIA_METADATA_MEDIA_TYPE="Polaroid"
As a JavaScript Library
If you're using javascript, you can use this as a library like this:
const uploadMedia = require("@amonks/upload-media");
uploadMedia("path-to-my-file.mp4").then((success, err) => {
if (error) throw Error("Error uploading media!\n" + error.message);
console.log("Success!");
});
What it does
It makes two requests. First, it sends an HTTP request to the UPLOAD_MEDIA_ENDPOINT_URL:
POST /upload HTTP/1.1
x-api-key: abc123
accept: application/json
content-type: application/json
accept-encoding: gzip,deflate
connection: close
content-length: 114
Host: whatever.com
{"contentLength":361,"contentType":"application/json","uuid":"ffdf5a2c-f28d-4b67-b1d4-cdd5af58a6dc","metadata":{"mediaType":"Polaroid"}}
It expects to receive a response like this:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 926
Connection: close
{"version":"1.0","responseId":"49cca5e7-9283-4099-9ed3-9b323d5c3a5d","sentAt":"2018-06-05T07:07:32.584Z","status":"ok","data":{"signedRequest":"https://xxxxxxxxxxxxxxxxxxxx-media-uploads.s3.us-west-2.amazonaws.com/ffdf5a2c-f28d-4b67-b1d4-xxxxxxxxxxxx/hhhhhhhhh?AWSxccessKeyId=ASIAIFNO2XZXXXXXXXXA&Content-Type=image%2Fjpeg&Expires=1528183351&Signature=NN2XXXXXXXXXXXXXR4QAGD71u3g%3D&x-amz-security-token=FQoDYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXkYeP5N10WNrd9otA4HvCtSVSgyFJLws3FW9GqU4CpU5DM7qO678YSpWdNJp3u%2BPjUnzQsC6apSN2jKKAD9tJBYOhNjPlpdBEt1KjlZbcCOmWjU4IU4cFw%2FnhTVVy7Zoe9%2FVLt3BT9Tls44iElxNzngHl7tMFXVOCORaG6A%2Fvx0mwzJGLkLOPpzb5YD97de%2BvbnGAE4SiAx9jYBQ%3D%3D","key":"ffdf5a2c-f28dxxxxxxxxxxxxxxxxx58a6dc/undefined","bucket":"whatever-media-uploads"}}
It then PUT
s the file to the url from the signedRequest field of the response.