繁体   English   中英

将图像上传到Azure Blob存储

[英]uploading image to azure blob storage

我知道这个问题可以解释为重复,但是我无法使blop服务正常工作。 我遵循了msdn上的标准示例 我已经在代码中实现了,但是遵循了示例。 我可以使用示例中提供的脚本获取MobileService,以插入具有开放属性的Blob。 然后,我使用以下代码将图像上传到Blob存储:

 BitmapImage bi = new BitmapImage();
 MemoryStream stream = new MemoryStream();
 if (bi != null)
 {
      WriteableBitmap bmp = new WriteableBitmap((BitmapSource)bi);
      bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
 }

 if (!string.IsNullOrEmpty(uploadImage.SasQueryString))
 {
       // Get the URI generated that contains the SAS 
       // and extract the storage credentials.
       StorageCredentials cred = new StorageCredentials(uploadImage.SasQueryString);
       var imageUri = new Uri(uploadImage.ImageUri);

       // Instantiate a Blob store container based on the info in the returned item.
       CloudBlobContainer container = new CloudBlobContainer(
       new Uri(string.Format("https://{0}/{1}",
       imageUri.Host, uploadImage.ContainerName)), cred);

       // Upload the new image as a BLOB from the stream.
       CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(uploadImage.ResourceName);
       await blobFromSASCredential.UploadFromStreamAsync(stream);//error!

       // When you request an SAS at the container-level instead of the blob-level,
       // you are able to upload multiple streams using the same container credentials.

       stream = null;
 }

我在标记为error的位置出现此代码错误,出现以下错误:

+       ex  {Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.

我不明白,因为从脚本返回字符串的代码是:

// Generate the upload URL with SAS for the new image.
var sasQueryUrl = blobService.generateSharedAccessSignature(item.containerName, 
item.resourceName, sharedAccessPolicy);

// Set the query string.
item.sasQueryString = qs.stringify(sasQueryUrl.queryString);

// Set the full path on the new new item, 
// which is used for data binding on the client. 
item.imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path;

当然,这也说明我并不完全掌握Blob存储的构造。 因此,任何帮助将不胜感激。

注释说明从服务器代码开始,它应至少创建5分钟的公共注释。 因此不是问题。 我的服务器脚本与链接相同。 但是在这里复制:

var azure = require('azure');
var qs = require('querystring');
var appSettings = require('mobileservice-config').appSettings;

function insert(item, user, request) {
// Get storage account settings from app settings. 
var accountName = appSettings.STORAGE_ACCOUNT_NAME;
var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
var host = accountName + '.blob.core.windows.net';

if ((typeof item.containerName !== "undefined") && (
item.containerName !== null)) {
    // Set the BLOB store container name on the item, which must be lowercase.
    item.containerName = item.containerName.toLowerCase();

    // If it does not already exist, create the container 
    // with public read access for blobs.        
    var blobService = azure.createBlobService(accountName, accountKey, host);
    blobService.createContainerIfNotExists(item.containerName, {
        publicAccessLevel: 'blob'
    }, function(error) {
        if (!error) {

            // Provide write access to the container for the next 5 mins.        
            var sharedAccessPolicy = {
                AccessPolicy: {
                    Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
                    Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
                }
            };

            // Generate the upload URL with SAS for the new image.
            var sasQueryUrl = 
            blobService.generateSharedAccessSignature(item.containerName, 
            item.resourceName, sharedAccessPolicy);

            // Set the query string.
            item.sasQueryString = qs.stringify(sasQueryUrl.queryString);

            // Set the full path on the new new item, 
            // which is used for data binding on the client. 
            item.imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path;

        } else {
            console.error(error);
        }

        request.execute();
    });
} else {
    request.execute();
}
}

图片的想法是该应用程序的其他用户应该可以访问它们。 据我了解,我已将其公开,但只公开发表了5分钟。 我保存在移动服务表中(需要对用户进行身份验证)的Blob网址,我希望在存储上具有相同的安全性。 但是不知道这是否完成了吗? 对于所有愚蠢的问题,我感到抱歉,但是我无法自己解决它,因此我不得不“似乎”愚蠢:)

如果有人最终在这里需要帮助。 我的问题是uri。 它应该是http而不是https。 然后没有错误上传。

但是,即使在工具箱中的测试图像控件上也无法显示图像。 问题是我必须将流设置为开始:

stream.Seek(0, SeekOrigin.Begin);

然后上传成功,并且能够检索数据。

暂无
暂无

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

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