Firebase Storage - error with uploading png image via Python google-cloud-storage lib - google-cloud-storage

I'm running web app based on Firebase Realtime Database and Firebase Storage.
I need to upload new images to Firebase google bucket every hour via Python google-cloud-storage lib.
Here are the docs.
My code for image upload (img_src path is correct):
bucket = storage.bucket()
blob = bucket.blob(img_src)
blob.upload_from_filename(filename=img_path, content_type='image/png')
Image seem to be uploaded successfully, but when manually viewing it in Firebase Storage, it doesn't load. All the image's specs seem to be correct. Please compare specs of manually uploaded image (loads fine) with corrupted one.
Thanks for help!

Whenever you upload an image using Firebase Console, an access token will be automatically generated. However, if you upload an image using any Admin SDK or gsutil you will need to manually generate this access token yourself.
Here is an example on how to generate and set an access token for an image using the Admin Python SDK.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
# Import UUID4 to create token
from uuid import uuid4
cred = credentials.Certificate("path/to/your/service_account.json")
default_app = firebase_admin.initialize_app(cred, {
'storageBucket': '<BUCKET_NAME>.appspot.com'
})
bucket = storage.bucket()
blob = bucket.blob(img_src)
# Create new token
new_token = uuid4()
# Create new dictionary with the metadata
metadata = {"firebaseStorageDownloadTokens": new_token}
# Set metadata to blob
blob.metadata = metadata
# Upload file
blob.upload_from_filename(filename=img_path, content_type='image/png')
Here is quick explanation:
Import the UUID4 library to create a token. from uuid import uuid4
Create a new UUID4. new_token = uuid4()
Create a new dictionary with the key-value pair. metadata = {"firebaseStorageDownloadTokens": new_token}
Set it as the blob's metadata. blob.metadata = metadata
Upload the file. blob.upload_from_filename(...)
This solution can be implemented for any Admin SDK.
Firebase Support says that this is being fixed, but I think anyone having this problem should go this way instead of waiting for Firebase to fix this.

Related

AWS S3 storage does not give a valid URL after uploading file

I am new to AWS. I use Amplify to upload a video file to S3 storage using Flutter. I want to get the URL after uploading a video to use it elsewhere. I use Amplify.Storage.getUrl(key)).url to get the URL. But when I want to go to the link via the browser, it shows the following error there:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>No AWSAccessKey was presented.</Message>
<RequestId>AK49EW70AR8N1NVC</RequestId>
<HostId>Z6FBLU/GABRvJKFX827m3HoIKfVIpU0iXmH3gwSpcu04nNiqqEFjHZGGLn3VyyVNMY7ndK541ro=
</HostId>
</Error>
And also this link doesn't work when I want to play a video using VideoPlayerController.network(videoUrl).
So what is the solution to get a general URL for the video file uploaded that can be used elsewhere? Thanks in advance.

Read the contents of a .csv file in Google Drive

I have a public .csv file on my Google Drive and I want to pull the file and display it in my flutter app. I figured that I need the fileID, the OAuth token and the client ID,I already got these. I also have a fully working sign-in page. But I can't figure out what is the exact methodology of fetching the file. I know that there is some content of this exact question online, I am a beginner, those posts were old, and some functions were deprecated, I couldn't understand them.
You can use the export api to get this
"https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"
Refer https://developers.google.com/drive/api/v3/reference/files/export for more details
Please note that is restricted to 10 Mb per file
Edit
add these dependencies
dependencies:
googleapis: any
google_sign_in: any
Implement google signing
final googleSignIn = signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.DriveScope]);
final signIn.GoogleSignInAccount account = await googleSignIn.signIn();
Create a firebase project and enable google signin and link it with your project. Download its Json and add it to the project too.
Goto cloud console and enable Google Drive API
After the login is complete you can use the download method to download the file
Future<File> download(String id, String filename, {Function(int, int) onDownloadProgress});
Check this tutorial for reference
https://betterprogramming.pub/the-minimum-guide-for-using-google-drive-api-with-flutter-9207e4cb05ba

How does Alamofire "upload" images from Swift to a backend?

I am trying to upload images from Swift to my Django backend so that I can store them in Google Cloud Storage and access them as "foreign keys" in my database models in django. There seems to be a built in library to bridge Django models and Google Storage https://django-storages.readthedocs.io/en/latest/backends/gcloud.html.
Reference upload image to server using Alamofire.
Before sending an image to the backend, the image is converted into a UIImageJPEGRepresentation. How exactly is Alamofire then sending the image via "multipartFormData"? Is it simply sending the raw UIImageJPEGRepresentation as a string?
I am asking this because I need to somehow convert this image data I receive in my backend and store it in a Django model and simultaneously upload it to Google Storage.
From the documentation:
>>> class Resume(models.Model):
... pdf = models.FileField(upload_to='pdfs')
... photos = models.ImageField(upload_to='photos')
To store a local file to cloud storage and at the same time in a model field:
>>> obj1.pdf.save('django_test.txt', ContentFile('content'))
>>> obj1.pdf
<FieldFile: tests/django_test.txt>
Would something like this work for an image sent from Alamofire?
>>> obj1.photos.save(<Insert whatever was sent from Alamofire>)
If so, how do you access the data sent from Alamofire?

Get s3 filename from filepicker.io after successful upload

I've been using Filepicker.IO in order to upload files directly from the browser to the amazon s3 and most things are working fine, the only problem i'm facing now is that after the upload is done, i'm not getting the name of the file in the s3.
Filepicker js api is returning this object:
Object {url: "https://www.filepicker.io/api/file/xxxxxxxxxxxxx", filename: "xyzhi.mp4", mimetype: "video/mp4", size: 36735, isWriteable: true}
Usually this object comes with a property named 'key' which has the name of the file in the S3.
This happens when the upload is not done from the local computer, if i pick a local file everything works ok, but if i pick a file from any of the providers (e.g Dropbox, Google Drive), i can't get the filename in the S3 server.
Thanks.
You should make sure that you are using a function that is explicitly storing to S3, for instance filepicker.pickAndStore or filepicker.store. As noted in the filepicker.io pick API documentation, the "key" parameter on fpfiles returned specifically from the .pick() call are deprecated and not meant to be used.

GWT GAE Upload through Blob

If I'm using GWT File widget and form panel, can someone explain how to handle upload on blobstore on google application engine??
Take a look at gwtupload. There are examples on how to use it with GAE Blobstore.
Google blobstore is specifically designed to upload and serve blobs via http. Blobstore service (obtained using BlobstoreServiceFactory.getBlobstoreService()) generates http post action for you to use in the html form. By posting file to it you upload your blob to the blobstore. When you generate this action you provide a path to the handler (servlet) where you have access to uploaded blob key:
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("data");
Note, that "data" is the file field in your form. All you have is a key to the blob (your file). From here you take control - you can save this key for later and/or immediately serve the blob on a page (using key):
BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
blobstoreService.serve(blobKey, res);
Of course, for details see Google documentation.
One nice feature of the blobstore that it's integrated with Google Mapper (rudimentary map-reduce) service (work in progress) which lets you process files uploaded as blobs line by line: http://ikaisays.com/2010/08/