我必须检查网络上传速度以及在 azure blob 存储中上传文件。
最初,我尝试使用一些示例 HTTP 服务器来检查网络速度,它是否正常工作。但是,当我尝试将文件发送到 azure blob storage HTTP 端点时,我不确定如何使用 SAS token 进行身份验证,或者用户名和密码足以进行身份验证并继续执行进一步的过程。
我已附上我编写的代码,请提供一些使用 SAS token 进行身份验证和文件上传的示例或可靠链接。
size_t uploadspeed()
{
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
curl_off_t fsize;
size_t speed_upload, total_time1;
hd_src = fopen("bbb.txt", "rb");
curl = curl_easy_init();
if(curl) {
int old_stdout = dup(2);
freopen("/dev/null" , "w+" , stderr);
FILE *f = fopen("target.txt", "wb");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curl, CURLOPT_URL,
"AZURE_URL");
curl_easy_setopt(curl, CURLOPT_USERPWD, "A:B");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
fclose(stderr);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time1);
}
curl_easy_cleanup(curl);
fclose(hd_src);
curl_global_cleanup();
return speed_upload;
}
}
最佳答案
Sumana,抱歉回复晚了,您应该考虑看看这个 documentation
以下是使用 SAS 和使用 C# 的 block blob 的示例:
string sasUri = "https://storagesample.blob.core.windows.net/sample-container/" +
"sampleBlob.txt?sv=2015-07-08&sr=b&sig=39Up9JzHkxhUIhFEjEH9594DJxe7w6cIRCg0V6lCGSo%3D" +
"&se=2016-10-18T21%3A51%3A37Z&sp=rcw";
CloudBlockBlob blob = new CloudBlockBlob(new Uri(sasUri));
// Create operation: Upload a blob with the specified name to the container.
// If the blob does not exist, it will be created. If it does exist, it will be overwritten.
try
{
MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
msWrite.Position = 0;
using (msWrite)
{
await blob.UploadFromStreamAsync(msWrite);
}
Console.WriteLine("Create operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Create operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
请告诉我这是否有帮助。
关于c - LibCurl : Upload file to azure blob storage and Authorization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003866/