How to upload file to virtual folder in blob container? - python

I'm currently working on a function to upload a file to a virtual folder that is in my blob container. E.g. I want to upload the file into "container1\folder" rather than "container1".
So far, I am able to upload the file directly into the container with the following code:
filename = "test.txt"
file_path = "C:\Users\____\Desktop\test.txt"
blob_client = blob_service_client.get_blob_client(container='container1', blob=filename)
with open(file_path, "rb") as data:
blob_client.upload_blob(data)
Any tips or suggestions on how to upload the file into a virtual folder would be much appreciated.

You can just prepend the path to the filename
var file_name = "file.txt";
var folder_name = "folder1";
var blobpath = f"{folder_name}/{file_name}";
file_path = "C:\Users\____\Desktop\test.txt"
blob_client = blob_service_client.get_blob_client(container='container1', blob=blob_path)
with open(file_path, "rb") as data:
blob_client.upload_blob(data)

Related

Download images from azure storage

I want to download all images in a folder from a container i tried to download one image and it works, i need just to put the name of the image (Exemple: download_Image(bird.png)). It works only with this path path="files/exemple.png".
def download_Image(blobname):
blob_service_client = BlobServiceClient.from_connection_string(azure_storage_setting)
blob_client = blob_service_client.get_blob_client(container="images", blob=blobname)
try:
path="files/exemple.png"
download_file_path = os.path.join(path)
message='Download done!'
os.makedirs(os.path.dirname(download_file_path), exist_ok=True)
with open(download_file_path, "wb") as download_file:
download_file.write(blob_client.download_blob().readall())
except ResourceNotFoundError:
print("No blob found.")
message = "No blob found."
return message
download_Image(a)
I just want to download images with original names so i changed the path like this path = "files" and i had this error:
FileNotFoundError: [WinError 3] Das System kann den angegebenen Pfad nicht finden: ''
I tried to reproduce in my environment works fine successfully.
I have created a path folder along with sample images in blob storage like below.
To download all images in a folder from a container in azure blob storage make use of below code to execute.
import os
from azure.storage.blob import BlobServiceClient, BlobClient
MY_CONNECTION_STRING = "CONNECTION_STRING"
MY_BLOB_CONTAINER = "name" eplace with the local folder where you want files to be downloaded
LOCAL_BLOB_PATH = "Blobsss"
BLOBNAME="test"
class AzureBlobFileDownloader:
def __init__(self):
print("Intializing AzureBlobFileDownloader")
self.blob_service_client = BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)
self.my_container = self.blob_service_client.get_container_client(MY_BLOB_CONTAINER)
def save_blob(self, file_name, file_content):
# Get full path to the file
download_file_path = os.path.join(LOCAL_BLOB_PATH, file_name)
os.makedirs(os.path.dirname(download_file_path), exist_ok=True)
with open(download_file_path, "wb") as file:
file.write(file_content)
def download_all_blobs_in_container(self):
my_blobs = self.my_container.list_blobs()
for blob in my_blobs:
print(blob.name)
bytes = self.my_container.get_blob_client(blob).download_blob().readall()
self.save_blob(blob.name, bytes)
azure_blob_file_downloader = AzureBlobFileDownloader()
azure_blob_file_downloader.download_all_blobs_in_container()
All images in the folder path are downloaded successfully like below.
Output:

How to upload a data frame to specific folder in SharePoint Using python?

buffer = BytesIO()
updated_sheet.to_excel(buffer, index=False)
buffer.seek(0)
file_content = buffer.read()
list_title = "Documents"
target_folder = ctx.web.lists.get_by_title(list_title).root_folder
I used this code to upload a file in root folder. But I want to upload in respective folder.
I tried changing the root folder options but I can't place a file.

Streamlit, Flask, Fastapi: How to upload zipped files to my web app and make them downloadable?

I am working on an app using streamlit to upload a zipped file and enable users to download that zipped file. For a reference example, see how online services take in a word file, convert it into pdf and make that pdf downloadable automatically.
I have been able to complete the first 2 steps. Requesting assistance with uploading the file and making it downloadable. Thank you.
This is how it is done.
import streamlit as st
# zipping the different parts of the song
def zipdir(dst, src):
zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print('zipping %s as %s' % (os.path.join(dirname, filename),
arcname))
zf.write(absname, arcname)
zf.close()
zip_path = 'Python.zip'
zipdir(zip_path, path)
# send to webapp to make downloadable link
with open(zip_path, "rb") as f:
bytes_read = f.read()
b64 = base64.b64encode(bytes_read).decode()
href = f'<a href="data:file/zip;base64,{b64}" download=\'{title}.zip\'>\
Click to download\
</a>'
st.sidebar.markdown(href, unsafe_allow_html=True)

how to download file from a blob and save in folder in python

i'm trying to download a .pdf file from a blob in python, but i can't figure it out how to save to a directory in my project folder. it keeps giving me an error permission denied.
the code:
block_blob_service = BlockBlobService(account_name='hi', account_key='hi')
tenant = 'hi'
container_name = 'files'
id_ = 1
upload_id = 'upload'+'/'+str(id_)
location = 'local'+'/'+tenant+'/'+upload_id+'/'
for c in block_blob_service.list_containers():
if c.name == container_name:
for blob in block_blob_service.list_blobs(c.name):
if location in blob.name:
print(blob.name)
block_blob_service.get_blob_to_path(c.name,blob.name,'data')
i can't save the pdf files that have in the "folder" to the folder data.
The third parameter of get_blob_to_path should be the file path and not the directory.
if './data' is an existing directory, get_blob_to_path will try to write a file into a directory which explains the Permission denied error.
Try with something like this:
import os
block_blob_service = BlockBlobService(account_name='hi',account_key='hi')
tenant = 'hi'
container_name = 'files'
id_ = 1
upload_id = 'upload'+'/'+str(id_)
location = 'local'+'/'+tenant+'/'+upload_id+'/'
for c in block_blob_service.list_containers():
if location in blob.name:
print(blob.name)
path_to_file = "data/" + blob.name
dir = os.path.dirname(path_to_file)
os.makedirs(dir, exist_ok=True)
# path_to_file should change between loop iterations
block_blob_service.get_blob_to_path(c.name,blob.name,'data')

UploadWriteFailed(reason=WriteError('disallowed_name', None)

I'm trying to upload a whole folder to dropbox but only the files get uploaded. Should I create a folder programatically or can I solve the folder-uploading so simple? Thanks
import os
import dropbox
access_token = '***********************'
dbx = dropbox.Dropbox(access_token)
dropbox_destination = '/live'
local_directory = 'C:/Users/xoxo/Desktop/man'
for root, dirs, files in os.walk(local_directory):
for filename in files:
local_path = root + '/' + filename
print("local_path", local_path)
relative_path = os.path.relpath(local_path, local_directory)
dropbox_path = dropbox_destination + '/' + relative_path
# upload the file
with open(local_path, 'rb') as f:
dbx.files_upload(f.read(), dropbox_path)
error:
dropbox.exceptions.ApiError: ApiError('xxf84e5axxf86', UploadError('path', UploadWriteFailed(reason=WriteError('disallowed_name', None), upload_session_id='xxxxxxxxxxx')))
[Cross-linking for reference: https://www.dropboxforum.com/t5/API-support/UploadWriteFailed-reason-WriteError-disallowed-name-None/td-p/245765 ]
There are a few things to note here:
In your sample, you're only iterating over files, so you won't get dirs uploaded/created.
The /2/files/upload endpoint only accepts file uploads, not folders. If you want to create folders, use /2/files/create_folder_v2. You don't need to explicitly create folders for any parent folders in the path for files you upload via /2/files/upload though. Those will be automatically created with the upload.
Per the /2/files/upload documentation, disallowed_name means:
Dropbox will not save the file or folder because of its name.
So, it's likely you're getting this error because you're trying to upload an ignored filed, e.g., ".DS_STORE". You can find more information on those in this help article under "Ignored files".

Categories

Resources