如何为移动应用程序同步表配置azure blob存储

时间:2016-12-22 06:09:11

标签: c# azure xamarin azure-blob-storage

我正在为Azure移动应用编写管理面板。后端使用Asp.net MVC和实体框架来创建和编辑数据库项目。

我知道在与表控制器通信时,xamarin应用程序使用SyncTable类的FileSync功能,以便上传和下载文件以及创建的数据库对象。

我想添加图像以与通过后端创建的数据库对象相对应。

我可以通过blob存储上传图像,但我不知道在哪里放置文件以便SyncTable.FileSync方法正确检索它。

ShopItem DataObject

public class ShopItem : EntityData
    {
        public decimal Price { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Note { get; set; }
    }

BlobHandler

public class BlobHandler
    {
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        private string imageDirectoryUrl;

        /// <summary>
        /// Receives the users Id for where the pictures are and creates 
        /// a blob storage with that name if it does not exist.
        /// </summary>
        /// <param name="imageDirectoryUrl"></param>
        public BlobHandler(string imageDirecoryUrl)
        {
            this.imageDirectoryUrl = imageDirecoryUrl;
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container. 
            CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);

            // Create the container if it doesn't already exist.
            container.CreateIfNotExists();

            //Make available to everyone
            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
        }

        public void Upload(IEnumerable<HttpPostedFileBase> file)
        {
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container. 
            CloudBlobContainer container = blobClient.GetContainerReference(imageDirectoryUrl);

            if (file != null)
            {
                foreach (var f in file)
                {
                    if (f != null)
                    {
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(f.FileName);
                        blockBlob.UploadFromStream(f.InputStream);
                    }
                }
            }
        }

        public List<string> GetBlobs()
        {
            // Create the blob client. 
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(imageDirectoryUrl);

            List<string> blobs = new List<string>();

            // Loop over blobs within the container and output the URI to each of them
            foreach (var blobItem in container.ListBlobs())
                blobs.Add(blobItem.Uri.ToString());

            return blobs;
        }
    }

发布图片的ActionResult

public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
        {
            BlobHandler bh = new BlobHandler("containername");
            bh.Upload(file);
            var blobUris = bh.GetBlobs();

            return RedirectToAction("Index", blobUris);
        }

0 个答案:

没有答案