简体   繁体   English

上传到 azure blob

[英]Upload to azure blob

I have a problem uploading files [images] to azure blob in REST API.我在将文件 [图像] 上传到 REST API 中的 azure blob 时遇到问题。 my code in PHP:我在 PHP 中的代码:

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://STORAGE.blob.core.windows.net/PATH/NAME.png Shared_Access_Signature",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'PUT',
  CURLOPT_POSTFIELDS =>$postfields['file'],
  CURLOPT_HTTPHEADER => array(
    'x-ms-blob-type: BlockBlob',
    'Content-Type: multipart/form-data',   
    'Content-Type: image/png'
  ),
));

I see the file in the blob, but the file is invalid - the size of the original file is 200KB, but I see that the file size in BLOB is 600B.我在 blob 中看到文件,但文件无效 - 原始文件的大小为 200KB,但我看到 BLOB 中的文件大小为 600B。 What is the problem?问题是什么?

Thanks!谢谢!

You can try the following code snippet by using Shared Key Authentication to upload an image to azure blob.您可以通过使用共享密钥身份验证将图像上传到 azure blob 来尝试以下代码片段。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
date_default_timezone_set('UTC');

$destinationURL = 'https://mystorage.blob.core.windows.net/blob1/image.jpg';
$accesskey = "qJXTMmw2Esal8/tXgfWk0RLwMNJ6OQKEt0E8EMZ8VhQrzGi5uqJqeKvi1l7iqnOddp7bdtai5rPpC6ynHttl1w==";
$storageAccount = 'mystorage';
$filetoUpload = realpath('./image.jpg');

function upload($filetoUpload, $storageAccount,$destinationURL,$accesskey) {
    $currentDate = date("D, d M Y H:i:s");

    $postFields = array(
        'extra_info' => '123456',
        'file_contents'=>'@'.$filetoUpload
    );

    $headerText=""
        ."x-ms-version: 2015-02-21\n"  
        ."x-ms-date:" .$currentDate." GMT\n"
        ."Content-Type: text/plain; charset=UTF-8\n"  
        ."x-ms-blob-content-disposition: attachment; filename=".$filetoUpload."\n"  
        ."x-ms-blob-type: BlockBlob\n"  
        ."x-ms-meta-m1: v1\n"  
        ."x-ms-meta-m2: v2\n"  
        ;

    $hash = hash_hmac('sha256', $headerText, base64_decode($accesskey), true);
    $sig = base64_encode($hash);

    $headerText.="Authorization: SharedKey ".$storageAccount.":".$sig."\n";
    $headerText.="Content-Length: 280";
    $headers = explode("\n", $headerText);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_URL, $destinationURL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    echo ('Result<br/>');
    print_r($result);
    echo ('Error<br/>');
    print_r(curl_error($ch));
    curl_close ($ch);
return;
}

upload($filetoUpload, $storageAccount,$destinationURL,$accesskey);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM