Azure PUT Blob authentication fails in R
I managed to resolve this issue by putting the "\n" characters and everything in the right place.
Based on Gaurav Mantri's help, I used:
https://learn.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
The following changes in the 'signature_string' worked:
signature_string <- paste0("PUT", "\n", # HTTP Verb
"\n", # Content-Encoding
"\n", # Content-Language
content_length, "\n", # Content-Length
"\n", # Content-MD5
"text/plain", "\n", # Content-Type
"\n", # Date
"\n", # If-Modified-Since
"\n", # If-Match
"\n", # If-None-Match
"\n", # If-Unmodified-Since
"\n", # Range
# Here comes the Canonicalized Headers
"x-ms-blob-type:BlockBlob","\n",
"x-ms-date:",requestdate,"\n",
"x-ms-version:2015-02-21","\n",
# Here comes the Canonicalized Resource
"/",account, "/",container,"/", filename)
Error connecting to azure blob storage API from R
Looks like your problem is with the key. The string of the key you have provided is actually base64 encoded. You need to decode that to the raw vector before you use it to sign the request. For example:
url<-"https://preconstuff.blob.core.windows.net/pings?restype=container&comp=list"
sak<-"Q8HvUVJLBJK+wkrIEG6LlsfFo19iDjneTwJxX/KXSnUCtTjgyyhYnH/5azeqa1bluGD94EcPcSRyBy2W2A/fHQ=="
requestdate<-format(Sys.time(),"%a, %d %b %Y %H:%M:%S %Z", tz="GMT")
signaturestring<-paste0("GET",paste(rep("\n",12),collapse=""),
"x-ms-date:",requestdate,"
x-ms-version:2009-09-19
/preconstuff/pings
comp:list
restype:container")
headerstuff<-add_headers(Authorization=paste0("SharedKey preconstuff:",
RCurl::base64(digest::hmac(key=RCurl::base64Decode(sak, mode="raw"),
object=enc2utf8(signaturestring),
algo= "sha256", raw=TRUE))),
`x-ms-date`=requestdate,
`x-ms-version`= "2009-09-19")
content(GET(url,config = headerstuff, verbose() ))
There are no more authentication errors this way, though no blobs are listed. Perhaps that's a different issue.
Also, I changed the way the date/time was created to more "safely" change the local time to GMT.
Azure cli authentication with BlobServiceClient
You will have to use AzureCLICredentials
instead of using AzureCLIAuthentication
.
You can use something like below after doing a az login
:
from azure.identity import AzureCliCredential
from azure.storage.blob import BlobServiceClient
cli_auth = AzureCliCredential()
blob_service_client = BlobServiceClient(account_url="https://<Storageaccountname>.blob.core.windows.net", credential=cli_auth)
container_client = blob_service_client.get_container_client("<ContainerName>")
blobs=container_client.list_blobs()
for blob in blobs:
print(blob.name)
Output:
Authentication Failure when Accessing Azure Blob Storage through Connection String
I can work the following code well with the version the same as yours.
from azure.storage.blob import BlobServiceClient
blob=BlobServiceClient.from_connection_string(conn_str="your connect string in Access Keys")
with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)
Please check your connect-string, and check your PC's time.
There is a similar issue about the error: AzureStorage Blob Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature
UPDATE:
I tried with this code, and get the same error:
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential
token_credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url="https://pamelastorage123.blob.core.windows.net/",credential=token_credential)
blob_client = blob_service_client.get_blob_client(container="pamelac", blob="New Text Document.txt")
with open("D:/demo/python/New Text Document.txt", "rb") as data:
blob_client.upload_blob(data)
Then I use AzureCliCredential()
instead of DefaultAzureCredential()
. I authenticate via the Azure CLI with az login
. And it works.
If you use environment credential, you need to set the variables. Anyway, I recommend you to use the specific credentials instead DefaultAzureCredential
.
For more details about Azure Identity, see here.
Upload Block Blob to Azure Storage via SDK - Server failed to authenticate the request
Thanks to @Tom Sun - MSFT I have been sent to the right direction.
I now compared all dependencies between the new ASP.Net Core app which I started from scratch and my actual solution.
The problem was the following package:
Microsoft.ApplicationInsights.AspNetCore
If I used the 2.1.0-beta I was faced with the error.
Switching to the last stable 2.0.0 solved my problem
Azure Block Blob PUT fails when using HTTPS
Thank you for reporting this and we apologize for the inconvenience. We have managed to recreate the issue and have filed a bug. Unfortunately we cannot share a timeline for the fix at this time, but we will respond to this forum when the fix has been deployed. In the meantime, a plausible workaround (and a recommended best practice) is to break large uploads into smaller chunks (using the Put Block and Put Block List APIs), thus enabling the client to parallelize the upload.
Related Topics
How to Interpret Lm() Coefficient Estimates When Using Bs() Function for Splines
Simplest Way to Plot Changes in Ranking Between Two Ordered Lists in R
How Does Gganimate Order an Ordered Bar Time-Series
Save a Data Frame with List-Columns as CSV File
Round Vector of Numerics to Integer While Preserving Their Sum
Ordering Permutation in Rcpp I.E. Base::Order()
Pie Plot Getting Its Text on Top of Each Other
Installing Rmysql in Mavericks
R - How to Test for Character(0) in If Statement
How to Install Dependencies When Using "R Cmd Install" to Install R Packages
Adding Simple Legend to Plot in R
Generate a Sequence of Characters from 'A'-'Z'
How to Get Code Completion for R in Emacs Ess Similar to What Is Available in Rstudio
Display an Axis Value in Millions in Ggplot