Azure / azure-storage-python

Microsoft Azure Storage Library for Python
https://azure-storage.readthedocs.io
MIT License
338 stars 240 forks source link

¿Can I upload a file in memory to Azure File? #652

Closed pobs93 closed 4 years ago

pobs93 commented 4 years ago

Which service(blob, file, queue) does this issue concern?

File

Which version of the SDK was used? Please provide the output of pip freeze.

azure-storage-file==2.1.0

What problem was encountered?

I have created a .zip file in memory. If I write it into a file i can dowload it but I can´t find any way to upload it from the 'in memory' file

Have you found a mitigation/solution?

Note: for table service, please post the issue here instead: https://github.com/Azure/azure-cosmosdb-python.

xiafu-msft commented 4 years ago

Hi @pobs93 You can call create_file_from_path() API.

def run_sample():
    try:
        # Create the FileService that is used to call the File service for the storage account
        file_service_client = FileService(
            account_name='emilystageaccount', account_key='youraccountkey')

        # Create a share called 'quickstartshare'.
        share_name = '''quickstartshare'''
        file_service_client.create_share(share_name)

        # Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download.
        local_path = os.path.expanduser("~/Documents/Sample")
        local_file_name = "release.zip"
        full_path_to_file = os.path.join(local_path, local_file_name)

        print("Temp file = " + full_path_to_file)
        print("\nUploading to File storage as file" + local_file_name)

        # Upload the created file, use local_file_name for the file name
        file_service_client.create_file_from_path(
            share_name, None, local_file_name, full_path_to_file)

        # Download the file(s).
        # Add '_DOWNLOADED' as prefix to '.zip' so you can see both files in Documents.
        full_path_to_file2 = os.path.join(local_path, str.replace(
            local_file_name,'.zip', '_DOWNLOADED.zip'))
        print("\nDownloading file to " + full_path_to_file2)
        file_service_client.get_file_to_path(
            share_name, None, local_file_name, full_path_to_file2)

        sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
                         "application will exit.")
        sys.stdout.flush()
        input()

        # Clean up resources. This includes the container and the temp files
        file_service_client.delete_share(share_name)
        os.remove(full_path_to_file)
        os.remove(full_path_to_file2)
    except Exception as e:
        print(e)

# Main method.
if __name__ == '__main__':
    run_sample()