Search code examples
restazurepython-requestsazure-storageazure-storage-files

can't connect to azure file service REST api by python


I want to access my file service by python Request, guided by the list shares page, I'm new to REST and python Request. my input is

headers= {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongstorageaccount:codecodecodeFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'}
url = 'https://zedongstorageaccount.file.core.windows.net/?comp=list'
r=requests.get(url,headers=headers)

But get error, the output of r.content:

b'\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:ef98f282-f01a-0042-3e24-d62397000000\nTime:2018-04-17T08:16:21.9635335Z</Message><AuthenticationErrorDetail>The Date header in the request is incorrect.</AuthenticationErrorDetail></Error>'

I have also try the header {'x-ms-date': '2018-04-17 06:22:15.181620', 'Authorization': 'SharedKey zedongnewstorageaccount:NJYYtabOIj5D1R+xb0PpmXrjCldf6NA6oLkYReAKFiTzubX9tvC3G3PcDYzR2cX/TMjkOu4JhsvQffS+xTDDBQ==', 'x-ms-version': '2017-07-29'}, because i don't know if the Authorization and account in the same line. I also tried many version of x-ms-version. But there all the 400 or 403 respond.

I have read the Authentication for the Azure Storage Services but confused.

Where is the error? And is there case I can learn to build my app by Azure REST API?(I use the keyword to google, all the pages are about build REST API and azure official docs)


Solution

  • I also recommend to use Python SDK but for educational purposes:

    import requests
    import datetime
    import hmac
    import hashlib
    import base64
    
    storage_account_name = 'teststorageaccount'
    storage_account_key = 'D4x6YChpyfqWk9a.......'
    api_version = '2016-05-31'
    request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
    
    string_params = {
        'verb': 'GET',
        'Content-Encoding': '',
        'Content-Language': '',
        'Content-Length': '',
        'Content-MD5': '',
        'Content-Type': '',
        'Date': '',
        'If-Modified-Since': '',
        'If-Match': '',
        'If-None-Match': '',
        'If-Unmodified-Since': '',
        'Range': '',
        'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
        'CanonicalizedResource': '/' + storage_account_name + '/\ncomp:list'
    }
    
    string_to_sign = (string_params['verb'] + '\n' 
                      + string_params['Content-Encoding'] + '\n'
                      + string_params['Content-Language'] + '\n'
                      + string_params['Content-Length'] + '\n'
                      + string_params['Content-MD5'] + '\n' 
                      + string_params['Content-Type'] + '\n' 
                      + string_params['Date'] + '\n' 
                      + string_params['If-Modified-Since'] + '\n'
                      + string_params['If-Match'] + '\n'
                      + string_params['If-None-Match'] + '\n'
                      + string_params['If-Unmodified-Since'] + '\n'
                      + string_params['Range'] + '\n'
                      + string_params['CanonicalizedHeaders']
                      + string_params['CanonicalizedResource'])
    
    signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()
    
    headers = {
        'x-ms-date' : request_time,
        'x-ms-version' : api_version,
        'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
    }
    
    url = ('https://' + storage_account_name + '.file.core.windows.net/?comp=list')
    
    r = requests.get(url, headers = headers)
    
    print(r.content)
    

    Output:

    <?xml version="1.0" encoding="utf-8"?><EnumerationResults ServiceEndpoint="https://teststorageaccount.file.core.windows.net/"><Shares><Share><Name>myfileshare</Name><Properties><Last-Modified>Tue, 17 Apr 2018 13:43:00 GMT</Last-Modified><Etag>"0x8D5A46922CA8BDA"</Etag><Quota>10</Quota></Properties></Share></Shares><NextMarker /></EnumerationResults>