【问题标题】:uploading image to azure blob storage将图像上传到 azure blob 存储
【发布时间】:2014-06-04 02:42:02
【问题描述】:

我知道这个问题可以解释为重复,但我无法让 blop 服务正常工作。我遵循标准example on 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;
 }

我在标记为错误的地方收到此代码中的错误,并出现以下错误:

+       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 分钟的公共注释。因此不是问题。我的服务器脚本与the link 相同。但在这里复制:

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 分钟。我保存在 mobileservice 表中的 blob 的 url,用户需要在其中进行身份验证,我希望在存储上具有相同的安全性。但不知道这是否实现?对于所有愚蠢的问题,我很抱歉,但我无法自己解决,所以我不得不“看起来”很愚蠢:)

【问题讨论】:

  • 有点愚蠢的问题但是......容器存在吗?
  • 是的,它存在于存储中,这就是我无法理解的原因。并感谢您的理解
  • 我建议的一件事是通过 Fiddler 跟踪您的请求/响应。这应该会给你更多的信息。请告诉我们您的发现。
  • 该错误告诉您您尝试访问的 url 不在服务器上,请确保该 URL 正确。发送到CloudBlobContainer 的 uri 是什么?也许那里有一个// 导致了这个问题。我更喜欢做新的Uri(base, path) 之类的事情来避免这样的问题。
  • 如果容器是私有的,我会简单地尝试将其公开并删除凭据。这可能会使调试变得更容易。

标签: c# azure windows-phone-8 azure-mobile-services blobstorage


【解决方案1】:

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

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

stream.Seek(0, SeekOrigin.Begin);

然后上传工作并能够检索数据。

【讨论】:

  • 老实说,我不认为将 https 更改为 http 是一种解决方法,也许只是一个快速的 hack,但也许我错了
  • @LászlóCitrusNagy 你可能是对的,但它对我有用。但是,如果您有其他方法可以做到这一点,请随时告诉我,我当然会为您的麻烦投票;)
  • 我已经在做这个了,如果能完成的话,我会作为答案发布:)
  • @LászlóCitrusNagy 真棒;)
猜你喜欢
  • 2018-12-17
  • 2020-01-25
  • 2021-01-16
  • 2020-01-09
  • 2020-06-07
  • 2014-07-22
  • 2020-02-23
  • 1970-01-01
  • 2022-11-03
相关资源
最近更新 更多