Copy File From Azure Blob Into Azure Fileshare Directly Using Python

Pada posting berjudul "Copy File From Azure Blob Into Azure Fileshare Directly Using Python" ini penulis akan memberikan sample script menggunakan python cara untuk mengcopy data dari Azure Blob ke Azure Fileshare.

Pada umumnya jika kita ingin memindahkan data dari Azure Blob ke Fileshare adalah melakukan download blob ke local dahulu kemudian melakukan upload ke Fileshare. Tetapi pada sample kali ini kita akan menampung hasil download ke variabel dan kemudian melakukan upload variabel tersebut sebagai parameter data.
import os
from azure.storage.blob import BlockBlobService
from azure.storage.file import FileService
from azure.storage.file import ContentSettings
from datetime import datetime, timedelta

ACCOUNT_NAME = "myaccountname"
ACCOUNT_KEY = "Ckoo+MzpEI8bIlZO9R5hVgE0=="
BLOB_SOURCE = "blobsource"
FILESHARE_TARGET = "fstarget"

filename = 'part-00000-tid'
sourcefolder = 'sourceblobfolder'
targetfolder = 'targetfilesharefolder'

blob_service = BlockBlobService(account_name = ACCOUNT_NAME, account_key = ACCOUNT_KEY)
file_service = FileService(account_name = ACCOUNT_NAME, account_key = ACCOUNT_KEY)

try:
    blob_names = blob_service.list_blobs(BLOB_SOURCE)
    stoday = str(datetime.now())[:10].replace("-","")

    print('Find predictions files ....\n')
    for blob in blob_names:
        if filename in blob.name and sourcefolder in blob.name:
            print('Copy predictions', blob.name, '.... to blob variable (1/2)')
            target_filename = filename + '-' + stoday + '.csv'

            #copy into bytes
            result_blob = blob_service.get_blob_to_bytes(BLOB_SOURCE, blob.name, snapshot=None, start_range=None, end_range=None, validate_content=False, progress_callback=None, max_connections=2, lease_id=None, if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None, timeout=None)
            print('Upload predictions ', blob.name, '.... bytes blob into azure fileshare (2/2)')
            #copy to fileshare from bytes variable
            file_service.create_file_from_bytes(FILESHARE_TARGET, targetfolder, target_filename, result_blob.content, index=0, count=None, content_settings=None, metadata=None, validate_content=False, progress_callback=None, max_connections=2, timeout=None)

            source_file = 'https://' + ACCOUNT_NAME + '.file.core.windows.net/' + FILESHARE_TARGET + '/' + targetfolder + '/' + target_filename
            file_service.copy_file(FILESHARE_TARGET, targetfolder, filename + '.csv', source_file, metadata=None, timeout=None)
except Exception as exc:
    print('ERROR :', str(exc))

print("\nFinished ....")
Silahkan download sample script diatas di : Sample Script

Semoga sample program untuk "Copy File From Azure Blob Into Azure Fileshare Directly Using Python" diatas dapat bermanfaat.



Salam,
 

Popular posts from this blog

K2 Error Value Cannot Be Null. Parameter Name: s In SmartView and SmartObject

Cara inject USB 3.0 Driver pada instalasi Windows - How to Inject USB 3.0 Driver in Windows 7

Pentaho Spoon Error : "Could Not Create The Java Virtual Machine"