Thử cái này :
from zipfile import ZipFile
from azure.storage.blob import BlobServiceClient
import os,requests
tempPath = ''
if not os.path.isdir[tempPath]:
os.mkdir[tempPath]
zipFileName = 'test.zip'
storageConnstr = ''
container = ''
blob = BlobServiceClient.from_connection_string[storageConnstr].get_container_client[container].get_blob_client[zipFileName]
fileURLs = {'//cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg',
'//1812.img.pp.sohu.com.cn/images/blog/2009/11/18/18/8/125b6560a6ag214.jpg',
'//513.img.pp.sohu.com.cn/images/blog/2009/11/18/18/27/125b6541abcg215.jpg'}
def download_url[url, save_path, chunk_size=128]:
r = requests.get[url, stream=True]
with open[save_path, 'wb'] as fd:
for chunk in r.iter_content[chunk_size=chunk_size]:
fd.write[chunk]
zipObj = ZipFile[tempPath + zipFileName, 'w']
#download file and write to zip
for url in fileURLs:
localFilePath = tempPath + os.path.basename[url]
download_url[url,localFilePath]
zipObj.write[localFilePath]
zipObj.close[]
#upload zip
with open[tempPath + zipFileName, 'rb'] as stream:
blob.upload_blob[stream]
- Xóa khỏi các diễn đàn của tôi
Câu hỏi
Chào mọi người,
Tôi không thể thực hiện các hoạt động sau trên tệp zip với Python:
- created_blob_from_path - created_blob_from_bytes
- create_blob_from_BytesTôi luôn nhận được lỗi sau: & nbsp; unicodedecodeerror: asciiUnicodeDecodeError: ascii
Đây là những gì tôi đang cố gắng:
#1 Nhận Blob từ Azure Storage
azure_blob = block_blob_service.get_blob_to_path [container_name = blob_container_name, blob_name = blobname, file_path = "test.zip", open_mode = 'wb']
#Add một số tệp và tải lại Zip trở lại
Hốt tức
block_blob_service = blockblobservice [account_name = storage_account, account_key = storage_key]
f = open ['test.zip', "rb"] & nbsp; & nbsp; & nbsp; block_blob_service.create_blob_from_bytes [container_name = blob_container_name, blob_name = 'test.zip', blob = f.read []]
block_blob_service.create_blob_from_bytes[container_name=BLOB_CONTAINER_NAME, blob_name='test.zip', blob=f.read[]]block_blob_service.create_blob_from_path [container_name = blob_container_name, blob_name = "test.zip", file_path = "test.zip"] & nbsp; & nbsp; & nbsp; & nbsp;
Tôi có thể tải lên một zip mới mà tôi tạo thủ công nhưng tôi không thể tải lên tệp zip được tải xuống từ bộ lưu trữ Azure Blob.new zip which I create manually but I'm not able to upload the zip file which is downloaded from azure blob storage.
Bất kỳ sự giúp đỡ nào cũng được đánh giá cao,
Anton
- Được chỉnh sửa bởi Friday, ngày 10 tháng 6 năm 2016 12:57 PM Friday, June 10, 2016 12:57 PM
- Di chuyển qua Liu - MSFTFriday, ngày 1 tháng 7 năm 2016 1:30 Chủ đề Ammove Gary Liu - MSFT Friday, July 1, 2016 1:30 AM move thread
- Được chỉnh sửa Bygary Liu - MSFTFriday, ngày 1 tháng 7 năm 2016 1:31 Tiêu đề Amedit Gary Liu - MSFT Friday, July 1, 2016 1:31 AM edit title
Câu trả lời
Hi,
Cảm ơn bạn đã gửi bài ở đây!
Bạn có đang theo dõi tài liệu này để tải xuống và tải Blobs lên Azure Blob Storage không?
Vui lòng chạy lệnh bên dưới
PIP Cài đặt Azure -nâng cấp
Nó sẽ nâng cấp một vài thành phần, sau đó chạy lệnh của bạn. Và kiểm tra nó làm cho bất kỳ sự khác biệt.
Regards,
Vikranth S. & NBSP;
- Được đề xuất như câu trả lời Byvikranth Stuesday, ngày 28 tháng 6 năm 2016 8:27 AM vikranth s Tuesday, June 28, 2016 8:27 AM
- Được đánh dấu là câu trả lời của Liu - Msftthursday, ngày 30 tháng 6 năm 2016 6:20 sáng Gary Liu - MSFT Thursday, June 30, 2016 6:20 AM
Azure Storage Blobs cung cấp một giải pháp lưu trữ nhanh và hiệu quả chi phí cho dữ liệu không có cấu trúc. Đó là một giải pháp lý tưởng nếu bạn muốn phục vụ nội dung như hình ảnh. Cũng có thể tăng tốc hiệu suất phân phối nội dung bằng dịch vụ Azure CDN với nó.
Trước khi chạy các chương trình sau, hãy đảm bảo rằng bạn đã sẵn sàng trước các điều kiện tiên quyết. Trong các chương trình Python mẫu sau đây, tôi sẽ sử dụng Python SDK V12 mới nhất cho Blob lưu trữ Azure.
Cài đặt Python 3.6 trở lên. Trong Mac, sử dụng homebrew để cài đặt Python 3,
Brew Cài đặt Python3
Cài đặt thư viện máy khách lưu trữ Azure Blob cho gói Python,
PIP3 Cài đặt Azure-Storage-Blob-Người dùng
Sử dụng cổng thông tin Azure, tạo tài khoản Azure Storage V2 và container trước khi chạy các chương trình sau. Bạn cũng sẽ cần sao chép chuỗi kết nối cho tài khoản lưu trữ của bạn từ cổng thông tin Azure. Nếu bạn muốn truy cập công khai vào hình ảnh đã tải lên, hãy đặt mức truy cập công khai container thành "Blob [chỉ đọc ẩn danh cho Blobs]".
Cách tải tệp lên Azure Storage Blobs bằng Python
Chương trình sau đây cho thấy một trường hợp sử dụng điển hình trong đó bạn muốn tải lên số lượng lớn một bộ hình ảnh JPG từ một thư mục cục bộ lên thùng chứa Azure Blob. Lưu ý rằng đối với số lượng lớn các tệp, chương trình này có thể không hiệu quả vì nó tuần tự tải lên hình ảnh. Thay thế My_Connection_String, my_image_container và local_image_path trước khi chạy chương trình.
# upload_blob_images.py # Python program to bulk upload jpg image files as blobs to azure storage # Uses latest python SDK[] for Azure blob storage # Requires python 3.6 or above import os from azure.storage.blob import BlobServiceClient, BlobClient from azure.storage.blob import ContentSettings, ContainerClient # IMPORTANT: Replace connection string with your storage account connection string # Usually starts with DefaultEndpointsProtocol=https;... MY_CONNECTION_STRING = "REPLACE_THIS" # Replace with blob container. This should be already created in azure storage. MY_IMAGE_CONTAINER = "myimages" # Replace with the local folder which contains the image files for upload LOCAL_IMAGE_PATH = "REPLACE_THIS" class AzureBlobFileUploader: def __init__[self]: print["Intializing AzureBlobFileUploader"] # Initialize the connection to Azure storage account self.blob_service_client = BlobServiceClient.from_connection_string[MY_CONNECTION_STRING] def upload_all_images_in_folder[self]: # Get all files with jpg extension and exclude directories all_file_names = [f for f in os.listdir[LOCAL_IMAGE_PATH] if os.path.isfile[os.path.join[LOCAL_IMAGE_PATH, f]] and ".jpg" in f] # Upload each file for file_name in all_file_names: self.upload_image[file_name] def upload_image[self,file_name]: # Create blob with same name as local file name blob_client = self.blob_service_client.get_blob_client[container=MY_IMAGE_CONTAINER, blob=file_name] # Get full path to the file upload_file_path = os.path.join[LOCAL_IMAGE_PATH, file_name] # Create blob on storage # Overwrite if it already exists! image_content_setting = ContentSettings[content_type='image/jpeg'] print[f"uploading file - {file_name}"] with open[upload_file_path, "rb"] as data: blob_client.upload_blob[data,overwrite=True,content_settings=image_content_setting] # Initialize class and upload files azure_blob_file_uploader = AzureBlobFileUploader[] azure_blob_file_uploader.upload_all_images_in_folder[]
Tải lên số lượng lớn các tệp lên các đốm lưu trữ Azure bằng cách sử dụng Python
Chương trình Python sau đây là phiên bản cải tiến của chương trình trên. Chương trình này sử dụng một nhóm luồng để tải lên một số lượng hình ảnh được xác định trước song song. Lưu ý rằng chương trình sử dụng 10 làm số lượng nhóm luồng, nhưng bạn có thể tăng nó để tải lên nhanh hơn nếu bạn có đủ băng thông mạng. Nếu bạn không chỉ định loại nội dung, nó sẽ mặc định là ứng dụng/luồng octet.
# upload_blob_images_parallel.py # Python program to bulk upload jpg image files as blobs to azure storage # Uses ThreadPool for faster parallel uploads! # Uses latest python SDK[] for Azure blob storage # Requires python 3.6 or above import os from multiprocessing.pool import ThreadPool from azure.storage.blob import BlobServiceClient, BlobClient from azure.storage.blob import ContentSettings, ContainerClient # IMPORTANT: Replace connection string with your storage account connection string # Usually starts with DefaultEndpointsProtocol=https;... MY_CONNECTION_STRING = "REPLACE_THIS" # Replace with blob container MY_IMAGE_CONTAINER = "myimages" # Replace with the local folder which contains the image files for upload LOCAL_IMAGE_PATH = "REPLACE_THIS" class AzureBlobFileUploader: def __init__[self]: print["Intializing AzureBlobFileUploader"] # Initialize the connection to Azure storage account self.blob_service_client = BlobServiceClient.from_connection_string[MY_CONNECTION_STRING] def upload_all_images_in_folder[self]: # Get all files with jpg extension and exclude directories all_file_names = [f for f in os.listdir[LOCAL_IMAGE_PATH] if os.path.isfile[os.path.join[LOCAL_IMAGE_PATH, f]] and ".jpg" in f] result = self.run[all_file_names] print[result] def run[self,all_file_names]: # Upload 10 files at a time! with ThreadPool[processes=int[10]] as pool: return pool.map[self.upload_image, all_file_names] def upload_image[self,file_name]: # Create blob with same name as local file name blob_client = self.blob_service_client.get_blob_client[container=MY_IMAGE_CONTAINER, blob=file_name] # Get full path to the file upload_file_path = os.path.join[LOCAL_IMAGE_PATH, file_name] # Create blob on storage # Overwrite if it already exists! image_content_setting = ContentSettings[content_type='image/jpeg'] print[f"uploading file - {file_name}"] with open[upload_file_path, "rb"] as data: blob_client.upload_blob[data,overwrite=True,content_settings=image_content_setting] return file_name # Initialize class and upload files azure_blob_file_uploader = AzureBlobFileUploader[] azure_blob_file_uploader.upload_all_images_in_folder[]
Sử dụng đối tượng ContentSting, có thể đặt loại nội dung, mã hóa nội dung, nội dung MD5 hoặc điều khiển bộ đệm cho các đốm màu. Xem ở đây để biết đầy đủ các thuộc tính Content_Sinstall.