示例#1
0
        public async Task <BlobDownloadModel> DownloadBlob(String blobName, BlobHelper.Repository repo)
        {
            var ipAddress = GetIPAddress();
            var isDev     = ipAddress.StartsWith("192.168");

            if (isDev)
            {
                blobName = "dev-" + blobName;
            }
            // TODO: You must implement this helper method. It should retrieve blob info
            // from your database, based on the blobId. The record should contain the
            // blobName, which you should return as the result of this helper method.
            if (!String.IsNullOrEmpty(blobName))
            {
                var container = BlobHelper.GetBlobContainer(repo);
                var blob      = container.GetBlockBlobReference(blobName);

                // Download the blob into a memory stream. Notice that we’re not putting the memory
                // stream in a using statement. This is because we need the stream to be open for the
                // API controller in order for the file to actually be downloadable. The closing and
                // disposing of the stream is handled by the Web API framework.
                var ms = new MemoryStream();
                await blob.DownloadToStreamAsync(ms);

                // Strip off any folder structure so the file name is just the file name
                var lastPos  = blob.Name.LastIndexOf('/');
                var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1);

                // Build and return the download model with the blob stream and its relevant info
                var download = new BlobDownloadModel
                {
                    BlobStream      = ms,
                    BlobFileName    = fileName,
                    BlobLength      = blob.Properties.Length,
                    BlobContentType = blob.Properties.ContentType
                };

                return(download);
            }

            // Otherwise
            return(null);
        }
示例#2
0
        public Task DownloadToStream(String blobName, Stream str, BlobHelper.Repository repo)
        {
            var ipAddress = GetIPAddress();
            var isDev     = ipAddress.StartsWith("192.168");

            if (isDev)
            {
                blobName = "dev-" + blobName;
            }
            // TODO: You must implement this helper method. It should retrieve blob info
            // from your database, based on the blobId. The record should contain the
            // blobName, which you should return as the result of this helper method.
            if (!String.IsNullOrEmpty(blobName))
            {
                var container = BlobHelper.GetBlobContainer(repo);
                var blob      = container.GetBlockBlobReference(blobName);

                return(blob.DownloadToStreamAsync(str));
            }
            return(null);
        }
示例#3
0
        public async Task <BlobUploadModel> UploadBlob(String key, String mediaType, FileStream fileStream, BlobHelper.Repository repo)
        {
            var ipAddress = GetIPAddress();
            var isDev     = ipAddress.StartsWith("192.168");

            if (isDev)
            {
                key = "dev-" + key;
            }
            // Retrieve reference to a blob
            var blobContainer = BlobHelper.GetBlobContainer(repo);
            var blob          = blobContainer.GetBlockBlobReference(key);

            // Set the blob content type
            blob.Properties.ContentType = mediaType;

            // Upload file into blob storage, basically copying it from local disk into Azure
            fileStream.Position = 0;
            blob.UploadFromStream(fileStream);

            // Create blob upload model with properties from blob info
            var blobUpload = new BlobUploadModel
            {
                FileName        = blob.Name,
                FileUrl         = blob.Uri.AbsoluteUri,
                FileSizeInBytes = blob.Properties.Length
            };

            return(blobUpload);
        }