I am following this guide:
and it only specified how to upload text files, but I want to upload image instead. I got this image data from front end:
const contentType = 'image/png';
const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
image = b64toBlob(image, contentType);
//the above turns base64 into blobs
var data= new FormData();
data.append('image',image );
data.append('pointerlocation',pointerlocation);
$.ajax({
url: "/update",
data: data,
processData: false,
contentType: false,
type: "POST",
success: function(result) {
$("#testimg").attr({ "src": `data:image/png;base64,${image}` });
}
});
This is what I got right now in python
image=self.request.get('image')#gets image
pointerlocation=self.request.get('pointerlocation')#just location of where the image is on my website
upload_file(image,pointerlocation)
def upload_file(image,pointerlocation):
bucket_name = os.environ.get(
'mosaictest', app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name
filename = bucket + '/'+pointerlocation
write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1)
with cloudstorage.open(
filename, 'w', content_type='image/png',
retry_params=write_retry_params) as cloudstorage_file:
#pseudocode-----------------------------------
cloudstorage_file.addfile(image)
url=get public url of(filename)
return url
#---------------------------------------------------
I have been stuck on this question for over 2 weeks because google apprently dropped support for python 2.7 and this is the only thing that even came close to working