Skip to content

Instantly share code, notes, and snippets.

@RobBlackwell
Created November 19, 2013 15:54
Show Gist options
  • Save RobBlackwell/7547536 to your computer and use it in GitHub Desktop.
Save RobBlackwell/7547536 to your computer and use it in GitHub Desktop.
Python code to copy blobs between Windows Azure Storage accounts
#!/usr/bin/env python
import sys
import datetime
import time
from datetime import timedelta
sys.path.append('/home/reb/local/azure-sdk-for-python/')
from azure.storage import *
from azure.storage.sharedaccesssignature import *
blob_service1 = BlobService(account_name='account1', account_key='key1')
blob_service2 = BlobService(account_name='account2', account_key='key2')
def format_date_time (t):
return t.strftime('%Y-%m-%dT%H:%M:%SZ')
def sign (blob_service, container_name, blob_name):
sas = SharedAccessSignature(account_name=blob_service.account_name, account_key=blob_service.account_key)
start = datetime.now()
expiry = start + timedelta(minutes = 10)
accss_plcy = AccessPolicy()
accss_plcy.start = format_date_time (start)
accss_plcy.expiry = format_date_time (expiry)
accss_plcy.permission = 'r'
sap = SharedAccessPolicy(accss_plcy)
qry_str = sas.generate_signed_query_string(container_name + '/' + blob_name, RESOURCE_BLOB, sap)
qry_str2 = sas._convert_query_string(qry_str)
return blob_service.protocol + '://' + blob_service._get_host() + '/' + container_name + '/' + blob_name + '?' + qry_str2
# copy from source to destination
source = sign(blob_service2, 'junk', 'foo.txt')
blob_service1.copy_blob('foo', 'foo3.txt', source)
@dansalmo
Copy link

This example was certainly helpful but needed a few changes to work as is. First was setting the value for RESOURCE_BLOB:

RESOURCE_BLOB = 'r'

Second was using the utc time for expiry date. The documentation suggests not setting a start time for operations that you want to have start immediately. I used:

accss_plcy.expiry = (datetime.utcnow() + timedelta(minutes = 10)).strftime('%Y-%m-%dT%H:%M:%SZ')

If I did not use UTC time I got the following error:

(<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u'Unknown error (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.)\n\ufeff<?xml version="1.0" encoding="utf-8"?><Error><Code>CannotVerifyCopySource</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:832033bf-0040-4958-bd55-c2800ebddff0\nTime:2014-05-20T16:43:51.2823808Z</Message></Error>', 148, 149, 'ordinal not in range(128)'))

@NitinKeshavB
Copy link

Hi Rob,

i am having issues while i run this code.

i have pip installed all the latest packages for azure storage account in my system. Everytime i run the above script, i get this issue NameError: name 'SharedAccessPolicy' is not defined

i believe i have everything installed, could you tell me how to fix the SharedAccessPolicy issue.
Thanks! in advance

-Nitin

@irfixq
Copy link

irfixq commented Jun 7, 2021

Hi Rob,

i am having issues while i run this code.

i have pip installed all the latest packages for azure storage account in my system. Everytime i run the above script, i get this issue NameError: name 'SharedAccessPolicy' is not defined

i believe i have everything installed, could you tell me how to fix the SharedAccessPolicy issue.
Thanks! in advance

-Nitin

Hi,
I am experiencing the same issue. Do you have the solution yet?

@NitinKeshavB
Copy link

Hi Rob,
i am having issues while i run this code.
i have pip installed all the latest packages for azure storage account in my system. Everytime i run the above script, i get this issue NameError: name 'SharedAccessPolicy' is not defined
i believe i have everything installed, could you tell me how to fix the SharedAccessPolicy issue.
Thanks! in advance
-Nitin

Hi,
I am experiencing the same issue. Do you have the solution yet?

Couldn’t get this working hence created power shell script and used azcopy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment