Search This Blog

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

List GCS files in 3 steps using Python program

 


1) We should install google-cloud-storage package to access Google Cloud storage in our Python program. 

Just run the following pip install command to install google-cloud-storage

  pip install google-cloud-storage

 

2) The first thing to do to access GCS from your Python program is authentication. 

We'll create a credentials JSON file and use it for authentication. 

You can generate your own credentials JSON file using this link


Generate credentials JSON file

You can also follow this link to generate JSON file.

http://www.hadooplessons.info/2023/03/simple-way-to-generate-json-key-for.html


3)

Here are the crucial steps to write a Python program for accessing GCS:


create a client 

client = storage.Client()

get access to the existing bucket in GCS

bucket = client.get_bucket(bucket_name)

create file in the gcs bucket and write data into it.

blob = bucket.blob(file_name)

blob.upload_from_string(text)


The full code is given below.


from google.cloud import storage

import os

#from google.oauth2 import service_account

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "application_default_credentials.json"

#credentials=service_account.Credentials.from_service_account_file('./application_default_credentials.json')

def list_gcs_blobs(project_id,bucket_id):

    client = storage.Client(project=project_id)

    blobs=client.list_blobs(bucket_id)

    return blobs


project_id='startbig'

bucket_id='pubtest1'


blobs=list_gcs_blobs(project_id,bucket_id=bucket_id)

for blob in blobs:

    print('blob name is',blob)


Modify project_id,bucket_id and run the program. Please note you need to keep credentials JSON file in the same directory this program is written otherwise change the path in os.environ line.

Top 10 key differences between Java and Python

 Top two programming languages used in the software industry are Python and Java.Python tops the list and Java is close to Python.

Are you curious about questions such as which programming language, Java or Python, is faster? 

Wondering which language is superior for web development or overall usage? This article delves into the 10 crucial distinctions that every Java and Python developer should be aware of.


1.



Java is a compiled language. Entire Java code is converted into bytecode before it is run on a machine. This makes Java faster.

Whereas Python is an interpreted language, Python translates one line after another while is being run on the machine. this makes Python slower


2.



Java is a static-typed language. Every variable requires a datatype to be defined in Java and every method should have a return type.

Unlike Java, Python is a dynamically typed language. Python does not need a datatype for a variable and does not need a return type for a function.


3.



Java supports multiple inheritance partially through Interfaces. Python supports multiple inheritance fully. Python addresses the diamond problem using method resolution order.


4.



Java has several options for web development. Many frameworks like Spring support web development in Java. 

Python has fewer options for web development. Tools like FLASK and Django provide web development in Python.


5.



Python has advanced tools like Pandas and numpy for data analysis. data analysis tools in Java are not as popular as pandas and numpy.


6.



Java is the best option for database integration. Java has many ORM tools like hibernate and also Java supports enterprise applications.

Python has ORM tools like SQLAlchemy not many enterprise applications use Python.


7.



As discussed, Java is a static-typed language. Rapid prototyping and rapid development is difficult in Java. In Python prototyping and development tasks are faster.


8.



Java is fast and Python is slow. Python is an interpreted language and it translates code line after line. Python is slow because of that.

9.



Java is relatively less known for Artificial intelligence and Machine Learning applications. Python community has a rich library for artificial intelligence and machine learning applications.

10.



Java is well for mobile development. Whereas Python is not well-known for mobile development.

These are the top 10 differences between Java and Python. Let me know if you know of any more differences.