interface between google colaboratory and google cloud - python

From google colaboratory, if I want to read/write to a folder in a given bucket created in google cloud, how do I achieve this?
I have created a bucket, a folder within the bucket and uploaded bunch of images into it. Now from colaboratory, using jupyter notebook, want to create multiple sub-directories to organise these images into train, validation and test folders.
Subsequently access respective folders for training, validating and testing the model.
With Google drive, we just update the path to direct to specific directory with following commands, after authentication.
import sys
sys.path.append('drive/xyz')
We do some thing similar on desktop version also
import os
os.chdir(local_path)
Does some thing similar exist for Google Cloud Storage?
I colaboratory FAQs, it has procedure for reading and writing a single file, where we need to set the entire path. That will be tedious to re-organise a main directory into sub-directories and access them separately.

In general it's not a good idea to try to mount a GCS bucket on the local machine (which would allow you to use it as you mentioned). From Connecting to Cloud Storage buckets:
Note: Cloud Storage is an object storage system that does not have the
same write constraints as a POSIX file system. If you write data
to a file in Cloud Storage simultaneously from multiple sources, you
might unintentionally overwrite critical data.
Assuming you'd like to continue regardless of the warning, if you use a Linux OS you may be able to mount it using the Cloud Storage FUSE adapter. See related How to mount Google Bucket as local disk on Linux instance with full access rights.
The recommended way to access GCS from python apps is using the Cloud Storage Client Libraries, but accessing files will be different
than in your snippets. You can find some examples at Python Client for Google Cloud Storage:
from google.cloud import storage
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('bucket-id-here')
# Then do other things...
blob = bucket.get_blob('remote/path/to/file.txt')
print(blob.download_as_string())
blob.upload_from_string('New contents!')
blob2 = bucket.blob('remote/path/storage.txt')
blob2.upload_from_filename(filename='/local/path.txt')
Update:
The Colaboratory doc recommends another method that I forgot about, based on the Google API Client Library for Python, but note that it also doesn't operate like a regular filesystem, it's using an intermediate file on the local filesystem:
uploading files to GCS
downloading files from GCS:

Related

Uploading file to s3 using python and fiftyone api

I am trying to create an automated pipeline that gets files from this api fiftyone and load it to s3. From what I saw the fiftyone package can only download it locally.
import fiftyone as fo
import fiftyone.zoo as foz
dataset = foz.load_zoo_dataset(
"open-images-v6",
split="validation",
classes=["Cat","Dog"],
max_samples=100,
label_types=["detections"],
seed=51,
dataset_name="open-images-pets"
Thats the code I use to download the files, thing is they download locally. Anyone that has some experience with this and how could this be done?
Thank you!
You're right that the code snippet that you shared will download the files from Open Images to whatever local machine you are working on. From there, you can use something like boto3 to upload the files to s3. Then, you may want to check out the examples for using s3fs-fuse and FiftyOne to see how you can mount those cloud files and use them in FiftyOne.
Directly using FiftyOne inside of a Sagemaker notebook is in development.
Note that FiftyOne Teams has more support for cloud data, with methods to upload/download to the cloud and use cloud objects directly rather than with s3fs-fuse.

google cloud functions - permanent storage

I have a function on Google cloud written in python that download images from firebase storage than manipulates them, there is no reason for downloading these images all over again, all I want is to download the new images, is there an option to save the downloaded images for reuse instead of downloading them over and over again?
There is no permanent storage for Cloud Function instances, that's pretty much what you'd use Cloud Storage (or another cloud-based file/data storage option) for.
But each Cloud Functions instance does have a writeable, local /tmp directory that you can use for caching information between calls in that instance. Note though that the storage is backed by your Cloud Function's RAM, so you'll want to typically limit what you store in this temp storage.

Is it possible to create a custom channel using Google Cloud Storage?

I would like to create a custom channel but instead of using the local file system, use a GCS bucket to host the packages. I have not been able to find any documentation or resources that indicate whether this is possible and/or how to do it. Does Anaconda allow the indexing of a GCS bucket?
If Conda can write in a local file system, try to use gcsFuse.
Thanks to it, you mount a directory which represent your bucket. GCS fuse transforms the system file IO call to GCS API call. Be careful, these calls aren't free. If you perform a large number of call, it will cost, a little!
In addition, don't expect the same read/write performance as you can have local SSD storage. Here, it's API Call, and the latency is not null!
Thereby, it's transparent for Conda and you can use your bucket like this.

How to copy files from cloud storage to other cloud? For example, Google Drive to OneDrive

I want to copy the files from Google Drive to OneDrive using any APIs in Python. Google provides some APIs but I don't see anything related to copy the files to another cloud.
One way to achieve this is like download files from Google Drive using Google Drive API and again upload to OneDrive.
Please share some inputs if there is a way to achieve this.
If you are running both services on your desktop, you could just use python to move the files from one folder to another. See How to move a file in Python. By moving the file from your google drive folder to your onedrive folder, the services should automatically sync and upload the files.
(As an aside, another solution if you don't care how the problem gets solved a service like https://publist.app might be of use).

How to add subdirectory in google cloud storage using flask in python 2

Im using cloudstorage module for uploading files to google cloud storage since am using python 2.x. And I need to create a subdirectory in my bucket and list all the contents in webpage.
You don't create directories in Google Cloud Storage (GCS).
GCS is a flat namespace.
GCS buckets contain objects and object names may include "/".
See here for more details.
gsutil and the Console browser make it appear as if buckets contain directories.

Categories

Resources