使用Rest API将Azure复制Blob从一个存储帐户复制到同一预订中的另一个存储帐户 [英] Azure copy blobs from one storage account to another in the same subscription using rest API

查看:77
本文介绍了使用Rest API将Azure复制Blob从一个存储帐户复制到同一预订中的另一个存储帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Rest API和PHP将blob从一个存储帐户复制到同一订阅中的另一个存储帐户.我可以使用以下代码将一个容器中的一个副本Blob复制到同一存储帐户中的另一个容器中

I would like to copy blob from one storage account to another storage account in the same subscription using Rest API and PHP. I am able to do a copy blob from one container to another in the same storage account using the below code

<?php    
$date = gmdate('D, d M Y H:i:s \G\M\T');
$account_name = "accname";
$destcontainername = "destcontainer";
$blobname = "blob.png";
$sourcecontainer = "sourcecontainer";    
$account_key = "asdf";

$canonicalizedHeaders = "x-ms-copy-source:https://".$account_name.".blob.core.windows.net/".$sourcecontainer."/".$blobname."\nx-ms-date:$date\nx-ms-version:2015-04-05";
$canonicalizedResource = "/$account_name/$destcontainername/$blobname";

$arraysign = array();
$arraysign[] = 'PUT';                     /*HTTP Verb*/  
$arraysign[] = '';                        /*Content-Encoding*/  
$arraysign[] = '';                        /*Content-Language*/  
$arraysign[] = '';                        /*Content-Length (include value when zero)*/  
$arraysign[] = '';                        /*Content-MD5*/  
$arraysign[] = '';                        /*Content-Type*/  
$arraysign[] = '';                        /*Date*/  
$arraysign[] = '';                        /*If-Modified-Since */  
$arraysign[] = '';                        /*If-Match*/  
$arraysign[] = '';                        /*If-None-Match*/  
$arraysign[] = '';                        /*If-Unmodified-Since*/  
$arraysign[] = '';                        /*Range*/  
$arraysign[] = $canonicalizedHeaders;     /*CanonicalizedHeaders*/
$arraysign[] = $canonicalizedResource;    /*CanonicalizedResource*/

$stringtosign = implode("\n", $arraysign);

$signature = 'SharedKey'.' '.$account_name.':'.base64_encode(hash_hmac('sha256', $stringtosign, base64_decode($account_key), true));

$endpoint = 'https://'.$account_name.'.blob.core.windows.net';
$url = $endpoint.'/'.$destcontainername.'/'.$blobname;

$headers = [
    'x-ms-copy-source:https://'.$account_name.'.blob.core.windows.net/'.$sourcecontainer.'/'.$blobname,
    "x-ms-date:{$date}",
    'x-ms-version:2015-04-05',
    'Accept:application/json;odata=nometadata',
    'Accept-Charset:UTF-8',
    'Content-Length:0',
    "Authorization:{$signature}"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response  = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);        

echo '<pre>';print_r($response);

我想知道是否必须使用相同的副本blob rest API来完成它.如果是,则$canonicalizedHeaders$canonicalizedResource以及resquest rest API将是什么.我可以使用下面的powershell命令将blob从一个存储帐户复制到另一个存储帐户

I would like to know if i have to use the same copy blob rest API to accomplish it. If yes than what will be the $canonicalizedHeaders and $canonicalizedResource and resquest rest API. I am able to do copy blob from one storage account to another using the powershell command below

Start-AzureStorageBlobCopy -DestContainer $destinationContainerName -DestContext $destinationContext -SrcBlob $vhdName -Context $sourceContext -SrcContainer $sourceSAContainerName

推荐答案

您也将使用相同的代码在存储帐户之间复制Blob.在$canonicalizedResource保持不变的情况下,您需要在$canonicalizedHeadersx-ms-copy-source标头中包含源Blob的URL.

You would use the same code for copying blobs across storage accounts as well. While $canonicalizedResource remain the same, you would need to include the URL of the source blob in x-ms-copy-source header in $canonicalizedHeaders.

要记住的一件事是,您必须在公共区域访问在x-ms-copy-source标头中指定的源Blob URL.这意味着,如果您使用该URL并将其粘贴到浏览器的地址栏中,则应该可以访问该Blob.如果源blob容器的ACL为BlobContainer,则可以简单地指定blob URL(https://sourceaccountname.blob.core.windows.net/sourceblobcontainer/sourceblobname),但是,如果源blob容器的ACL为Private,则需要在以下位置创建Shared Access Signature (SAS)具有至少Read权限的源Blob,并使用SAS URL作为此标头的值.

One important thing to keep in mind is that the source blob URL that you specify in x-ms-copy-source header must be publicly accessible. What that means is that if you take that URL and paste that in browser's address bar, you should be able to access the blob. If the source blob container's ACL is Blob or Container, then you can simply specify the blob URL (https://sourceaccountname.blob.core.windows.net/sourceblobcontainer/sourceblobname) however if the source blob container's ACL is Private then you would need to create a Shared Access Signature (SAS) on the source blob with at least Read permission and use the SAS URL for this header's value.

要理解的另一件事是,跨存储帐户复制是异步操作.因此,当上面的代码成功执行时,blob复制操作将排队.您必须先检查复制操作是否已完成,然后再删除源Blob或对其进行任何更改,否则复制操作将失败.

Another thing to understand is that copying across storage accounts is an asynchronous operation. So when the code above executes successfully, the blob copy operation is queued. You must check if the copy operation has completed before deleting source blob or making any changes to that otherwise the copy operation will fail.

这篇关于使用Rest API将Azure复制Blob从一个存储帐户复制到同一预订中的另一个存储帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆