Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.6k stars 2.81k forks source link

Example for uploading dataurl type #25789

Closed DavidPaf closed 1 year ago

DavidPaf commented 2 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Need an example for uploading dataurl to azure file share with python. In the current article, it uses existing file, not data url https://docs.microsoft.com/en-us/python/api/overview/azure/storage-file-share-readme?view=azure-python#uploading-a-file

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

azure-sdk commented 2 years ago

Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Data Lake Storage Gen2:0.22771856,Docs:0.21537232,Subscription:0.10174035'

annatisch commented 2 years ago

Thanks for reporting @DavidPaf - just to clarify the specifics of the sample you are looking for, are you looking to create the file client pointing to the upload uri? For example:

client = ShareFileClient.from_file_url(file_uri_to_upload_to)
with open("./SampleSource.txt", "rb") as source_file:
    client.upload_file(source_file)

Or are you looking for a sample that demonstrates uploading from a different source, e.g. copying source data from a URL? Adding @jalauzon-msft and @vincenttran-msft for visibility.

azure-sdk commented 2 years ago

Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Docs:0.4618508,Data Lake Storage Gen2:0.22391312,Storage:0.05912905'

jalauzon-msft commented 2 years ago

Hi @DavidPaf David, to add to what Anna mentioned, perhaps you are looking for the way to copy a file from the Storage URL? If that's the case, you'll want to look into start_copy_from_url. You can also check out our sample for that.

If you could provide some more information about what you are looking for, we can try and help further. Thanks.

DavidPaf commented 2 years ago

Hello @jalauzon-msft and @annatisch ,

I am finding a way to upload image using based64 format (url string), (not blob or file). I used to use firebase storage to upload image but now I changed my cloud storage to Microsoft Azure, therefore I would love to migrate my code from using Firebase to Azure Fileshare.

Here is the example from Google Firebase: https://firebase.google.com/docs/storage/web/upload-files#upload_from_a_string

They show how to upload from a string:

// Data URL string const message4 = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message4, 'data_url').then((snapshot) => { console.log('Uploaded a data_url string!'); });

As a workaround, I needed to convert data URL string to a file with file name, file content, file type and use upload_file method.

Hope it is clear now.

Thank you

jalauzon-msft commented 2 years ago

Thanks for the follow-up @DavidPaf David, that cleared things up. I wasn't very familiar with the Data URL format.

The SDK doesn't currently support the Data URL format natively as it's mostly a web format used to encode data, like images, in strings. Currently the best approach would be to convert the data URL to its parts on the client side and upload that information with upload_file. It seems you've already done that but here's a sample I've worked up for completeness:

import base64
from azure.storage.fileshare import ContentSettings, ShareFileClient

def parse_data_url(data_url: str) -> tuple[str, bytes]:
    # Trim off data:
    data_url = data_url[5:]

    # Split metadata and data
    metadata, data = data_url.split(',')
    metadata = metadata.split(';')

    content_type = metadata[0]
    is_base64 = "base64" in metadata

    if is_base64:
        data = data.encode('utf-8')
        data = base64.b64decode(data)
    else:
        data = data.encode('utf-8')

    return content_type, data

content_type, data = parse_data_url(sample_url)

file_client = ShareFileClient.from_file_url("<file-url>", credential=ACCOUNT_KEY)
content_settings = ContentSettings(content_type=content_type)
file_client.upload_file(data, content_settings=content_settings)

The sample is fairly naive and may not handle all scenarios but should handle your sample.

I'm not sure if adding native support for Data URLs to the SDK itself is something we'd be interested in given the web nature of the data type. @annatisch, what do you think about adding support for Data URLs?

jalauzon-msft commented 1 year ago

Hi @DavidPaf David, apologies but after some internal discussions around this, we've decided that we aren't going to add native support for the Data URL type at this time given that it does not seem to be a very common type that we get requests for. Hopefully the sample I provided above and/or the code you have already put in place is enough to meet your needs.

I'm going to close this Issue but if more folks chime in here looking for this feature, we may reconsider in the future. Thanks!