Last active
May 10, 2019 17:43
-
-
Save williamsiuhang/f39cf69c4f091d7452e3bf8aaf7c2792 to your computer and use it in GitHub Desktop.
Uploading an image/jpeg into Google Cloud Storage w/ POST method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upload image using http POST & Google Cloud Storage API | |
var uploadUrl = 'https://www.googleapis.com/upload/storage/v1/b/wkndevents/o?uploadType=media&name=loremipsum.jpg'; // upload with direct HTTP call | |
var tmp_auth = 'Bearer tokenhere123'; // Get Bearer Access token here - https://developers.google.com/oauthplayground | |
// Enable access for Cloud Storage v1 & Cloud Storage JSON API | |
var fileinput = $('.file-upload-input')[0]; // <input type='file'> | |
var file = fileinput.files[0]; // just support 1st file for now | |
// post image into google cloud storage | |
$.ajax({ | |
url: uploadUrl, | |
headers: { | |
'Authorization': tmp_auth, | |
'Content-Type': 'image/jpeg' // Only jpg support for now | |
}, | |
data: file, | |
processData: false, | |
contentType: false, | |
type: 'POST', | |
success: function(data){ | |
console.log(data); // Google Cloud Storage response | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment