‘vercel_blob’ is an unofficial Python Wrapper for the Vercel Blob Storage API. This article is a quick start on how to use vercel_blob with Python.
In a hurry? — TL;DR
Get all Blobs in the Storage: vercel_blob.list()
Upload a Blob in the Storage: vercel_blob.put(‘filename’, f.read())
Delete a Blob in the Storage: vercel_blob.delete(‘blob_url’)
Copy a blob to a new location inside the storage: vercel_blob.copy(“blob_url", “new/path/filename”)
Download a file in the server to a specified Directory: vercel_blob.download_file(‘blob_url’, ‘path/to/directory/’)
Documentation & Source: https://github.com/SuryaSekhar14/vercel_blob
Support & Donations: https://buymeacoffee.com/suryasekhar
Introduction
Flask is a lightweight web framework in Python. It lets you handle different web addresses, uses templates to easily design your web pages, and even has a built-in server for testing during development. It is light and easy.
On the other hand, a Blob Storage is a type of cloud storage designed for storing large amounts of unstructured data. We are obviously interested in Vercel’s Blob Storage offering.
Setup the basic Flask Application
This portion will be for people who are new to Flask and do not know how to set it up. If you are familiar, you can skip this part.
We will be assuming you already have >=Python3.11 installed, along with the Flask package. Create a file named ‘app.py’, and open it in your favorite IDE. Create another folder named ‘templates’ and place a file inside templates called ‘index.html’. In the interest of time, you can copy-paste the code below:
# app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello_world():
# Prepare some data to pass to the template
name = "World"
return render_template("index.html", name=name)
if __name__ == "__main__":
app.run(debug=True)
<!-- index.html -->
<!DOCTYPE html>
<html>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
If you wish to learn more about Flask, you can watch this video by freecodecamp.
How to work with ‘vercel_blob’
Now for the meat of this whole article, go ahead and install ‘vercel_blob’ using the command:
pip3 install vercel_blob
We also need a frontend to view our files and interact with them. Copy and paste the index.html below to follow along with me:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Files:</h1>
<form
method="post"
enctype="multipart/form-data"
action="/upload"
>
<input type="file" class="form-control" name="file" required />
<button type="submit">
Upload
</button>
</form>
<table>
<tr>
<th>Content Disposition</th>
<th>Content Type</th>
<th>Pathname</th>
<th>Size</th>
<th>Uploaded At</th>
<th>URL</th>
<th>Actions</th>
</tr>
{% for file in files %}
<tr>
<td>{{ file.contentDisposition }}</td>
<td>{{ file.contentType }}</td>
<td>{{ file.pathname }}</td>
<td>{{ file.size }}</td>
<td>{{ file.uploadedAt }}</td>
<td>{{ file.url }}</td>
<td>
<a href="{{ url_for('download', url=file.url) }}">Download</a>
<a href="{{ url_for('delete', url=file.url) }}">Delete</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
In the ‘app.py’ file, let’s write our first API. The home page API. Make sure to put your BLOB_READ_WRITE_TOKEN
in the .env file so that you can authenticate with the Blob Storage. If you do not have a Blob Store set up yet, create one right here.
# .env
BLOB_READ_WRITE_TOKEN="supersecretkey"
# app.py
import vercel_blob
from flask import Flask, request, render_template, redirect
import dotenv
app = Flask(__name__)
dotenv.load_dotenv()
@app.route('/')
def index():
resp = vercel_blob.list()
return render_template('index.html', files=resp.get('blobs'))
if __name__ == '__main__':
app.run()
In order to get all the blobs in the Blob Store, use the .list() method
Sometimes you might encounter something that looks like Request failed on attempt 1 (HTTPSConnectionPool(host=’blob.vercel-storage.com’, port=443): Read timed out. (read timeout=10))
. This is because Vercel Blob is still in Beta, and of course also because we are using an unofficial makeshift method :P
But once this works, you will see something like the image below.
To upload a new file to the Blob Store, use the .put() method
Let’s add a new API to the Flask app now.
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
print(file.filename)
vercel_blob.put(file.filename, file.read(), {})
return redirect('/')
When you upload a file to the input field on the webpage and click submit, your file will be uploaded to the Blob store.
To delete a file from the Blob Store, use the .delete() method
Here is the example API
@app.route('/delete', methods=['GET'])
def delete():
file_url = request.args.get('url')
vercel_blob.delete(file_url)
return redirect('/')
Now when you click the delete button, it will actually work and the file would be deleted from the Blob Store
To get all metadata about a file, use the .head() method
Suppose you want to make your user download the file. You would need to get the file metadata, and in the response, JSON is your download URL. You need to extract that URL and redirect your client to that URL, and Voilà!
For all that work, here is the API. The code should be self-explanatory.
@app.route('/download', methods=['GET'])
def download():
file_url = request.args.get('url')
download_url = vercel_blob.head(file_url).get("downloadUrl")
print(download_url)
return redirect(download_url)
To download a file to the server, use the .download_file() method
Now you know how to make your client download the file. But what if you want to download it on the server itself? Suppose you want to download it in a folder called ‘cache/’?
Here is the Python code to do that as well.
def download_a_file_on_the_server(blob_url):
vercel_blob.download_file(blob_url, 'path/to/directory/')
As you might already understand from reading the single-line function, you give it the Blob URL and the Path where it should download the file (it Defaults to the script’s directory), and the job is done!
To copy a file already in the Blob Store to a new path, use the .copy() method
Now here is a more refined use case. Suppose your user uploaded a file, but he wants to shift it to a new directory inside the Blob Store. There is just one single function that ‘vercel_blob’ offers to help you with it.
Let’s add an API for that too:
@app.route('/copy', methods=['GET'])
def copy():
file_url = request.args.get('url')
new_path = request.args.get('new_path')
vercel_blob.copy(file_url, new_path)
return redirect('/')
This method moves the specified Blob to the New Path inside the Blob Store.
Conclusion
Try playing around with the library and read about all the functionalities. There is much more to learn about Vercel Blob Storage, and you can check out their Official Page to learn more!
Feel free to contribute to ‘vercel_blob’, and if there are any issues, please open an Issue on GitHub. Also maybe, Buy me a Coffee?
Top comments (0)