将图像从 URL 上传到 Azure Blob 存储
Upload image to Azure Blob Storage from URL
是否可以让 CloudBlockBlob.UploadFromStreamAsync
接受 URL 图片?
我正在尝试使用 LinkedIn basicprofile api 来检索用户的图片 URL(已经有 URL)并且我正在尝试下载然后将图片上传到 Azure Blob ,就像他们从电脑中选择了一张图片。
这是现在的样子:
using (Html.BeginForm("UploadPhoto", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="browseimg">
<input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" />
</div>
}
<button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles">
Upload billede
</button>
控制器中的方法:
public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileExt = Path.GetExtension(file.FileName);
if (fileExt.ToLower().EndsWith(".png")
|| fileExt.ToLower().EndsWith(".jpg")
|| fileExt.ToLower().EndsWith(".gif"))
{
var user = await GetCurrentUserAsync()
await service.Upload(user.Id, file.InputStream);
}
}
return RedirectToAction("Index")
}
看来我只需要一个简单的
WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://media.licdn.com/mpr/..."));
然后
await service.Upload(user.Id, stream);
以下方法将文件(URL)上传到 azure cloudblob
注意:此方法示例的输入
file="http://example.com/abc.jpg" and ImageName="myimage.jpg";
public static void UploadImage_URL(string file, string ImageName)
{
string accountname = "<YOUR_ACCOUNT_NAME>";
string accesskey = "<YOUR_ACCESS_KEY>";
try
{
StorageCredentials creden = new StorageCredentials(accountname, accesskey);
CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer cont = client.GetContainerReference("<YOUR_CONTAINER_NAME>");
cont.CreateIfNotExists();
cont.SetPermissions(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream inputStream = response.GetResponseStream();
CloudBlockBlob cblob = cont.GetBlockBlobReference(ImageName);
cblob.UploadFromStream(inputStream);
}
catch (Exception ex){ ... }
}
是否可以让 CloudBlockBlob.UploadFromStreamAsync
接受 URL 图片?
我正在尝试使用 LinkedIn basicprofile api 来检索用户的图片 URL(已经有 URL)并且我正在尝试下载然后将图片上传到 Azure Blob ,就像他们从电脑中选择了一张图片。
这是现在的样子:
using (Html.BeginForm("UploadPhoto", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="browseimg">
<input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" />
</div>
}
<button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles">
Upload billede
</button>
控制器中的方法:
public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileExt = Path.GetExtension(file.FileName);
if (fileExt.ToLower().EndsWith(".png")
|| fileExt.ToLower().EndsWith(".jpg")
|| fileExt.ToLower().EndsWith(".gif"))
{
var user = await GetCurrentUserAsync()
await service.Upload(user.Id, file.InputStream);
}
}
return RedirectToAction("Index")
}
看来我只需要一个简单的
WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://media.licdn.com/mpr/..."));
然后
await service.Upload(user.Id, stream);
以下方法将文件(URL)上传到 azure cloudblob
注意:此方法示例的输入
file="http://example.com/abc.jpg" and ImageName="myimage.jpg";
public static void UploadImage_URL(string file, string ImageName)
{
string accountname = "<YOUR_ACCOUNT_NAME>";
string accesskey = "<YOUR_ACCESS_KEY>";
try
{
StorageCredentials creden = new StorageCredentials(accountname, accesskey);
CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer cont = client.GetContainerReference("<YOUR_CONTAINER_NAME>");
cont.CreateIfNotExists();
cont.SetPermissions(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream inputStream = response.GetResponseStream();
CloudBlockBlob cblob = cont.GetBlockBlobReference(ImageName);
cblob.UploadFromStream(inputStream);
}
catch (Exception ex){ ... }
}