GetBlockBlobReference() публичный Метод

Gets a reference to a block blob in this container.
public GetBlockBlobReference ( string blobAddressUri ) : CloudBlockBlob
blobAddressUri string The name of the blob, or the absolute URI to the blob.
Результат CloudBlockBlob
Пример #1
0
        //This function is using storage client ddl of blob
        public byte[] DownloadBlobClient(string UserId, string BlobName)
        {
            // Retrieve storage account from connection string.
            Microsoft.WindowsAzure.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

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


            //Set permission to public
            container.SetPermissions(
                new Microsoft.WindowsAzure.StorageClient.BlobContainerPermissions
            {
                PublicAccess =
                    Microsoft.WindowsAzure.StorageClient.BlobContainerPublicAccessType.Off
            });


            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);


            return(blockBlob.DownloadByteArray());
        }
Пример #2
0
        public void CopyFromBlob(string destinationSasUri, string srcBlobSasUri)
        {
            CloudBlockBlob blob = new CloudBlockBlob(srcBlobSasUri);
            string fileName = (blob.Name.Contains("/"))
                ? blob.Name.Substring(blob.Name.LastIndexOf("/"))
                : blob.Name;
            CloudBlobContainer cbc = new CloudBlobContainer(destinationSasUri);

            //UriBuilder ub = new UriBuilder(destUri);
            //ub.Path += "/" + fileName;
            //CloudBlockBlob destBlob = new CloudBlockBlob(ub.Uri);
            CloudBlockBlob destBlob = cbc.GetBlockBlobReference(fileName);
            BlobRequestOptions bro = new BlobRequestOptions();
            bro.RetryPolicy =  RetryPolicies.RetryExponential(5, TimeSpan.FromMilliseconds(150));
            destBlob.BeginCopyFromBlob(blob, bro, (result) => {  }, null);

               // destBlob.UploadFromStream(System.IO.File.OpenRead(@"D:\Install.txt"));

            System.Diagnostics.Debug.WriteLine(destBlob.Name);
        }
Пример #3
0
 public void PutChunk(CloudBlobContainer container, string filename, int chunk, Stream inputStream)
 {
     var cloudBlockBlob = container.GetBlockBlobReference(filename);
     var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes("chk_" + chunk.ToString("D8")));
     cloudBlockBlob.PutBlock(blockId, inputStream, null);
 }
Пример #4
0
        public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string configFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);

                if (configFilename == PackagerHelper.ConfigPackagerHelper.actualConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, configFilename));
                }
                else if (configFilename == PackagerHelper.ConfigPackagerHelper.desiredConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, configFilename));
                }

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    return(false);
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, configFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId);
                blockBlob.ServiceClient.Credentials.SignRequest(req);

                using (var reader = new BinaryReader(req.GetResponse().GetResponseStream()))
                {
                    FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate);
                    reader.BaseStream.CopyTo(zipFile);
                    zipFile.Close();
                }
                req.GetResponse().GetResponseStream().Close();

                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }
Пример #5
0
        public static bool UploadConfig(string configZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string desiredConfigFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    blockBlob.Container.CreateIfNotExist();
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new Microsoft.WindowsAzure.StorageClient.BlobProperties(), Microsoft.WindowsAzure.StorageClient.BlobType.BlockBlob, leaseId, 0);

                using (var writer = new BinaryWriter(req.GetRequestStream()))
                {
                    writer.Write(File.ReadAllBytes(configZipPath));
                    writer.Close();
                }

                blockBlob.ServiceClient.Credentials.SignRequest(req);
                req.GetResponse().Close();
                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("UploadConfig_Azure, configZipPath: " + configZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }
Пример #6
0
        /* format device */
        public void initialize(string id)
        {
            /* file system already initialized, return ASAP */
            if (_initialized == true)
            {
                return;
            }

            /*
             * no need to synchronize,
             * since when intialize the file system, only one process/thread
             * is active
             */
            try
            {
                Microsoft.WindowsAzure.CloudStorageAccount.
                    SetConfigurationSettingPublisher(
                        (configName, configSetter) =>
                        {
                            configSetter(RoleEnvironment.
                                GetConfigurationSettingValue(configName));
                        }
                    );
                var storageAccount =
                    CloudStorageAccount.FromConfigurationSetting(
                    "DataConnectionString");
                var client = storageAccount.CreateCloudBlobClient();
                /* get root path, create if not exists */
                var user_container = client.GetContainerReference("user");
                var blob = user_container.GetBlobReference("user/container/"+id);
                _rootPath = client.GetContainerReference(blob.DownloadText());
                _rootPath.CreateIfNotExist();
                Stream root = new MemoryStream();
                var root_blob = _rootPath.GetBlockBlobReference("/");
                root_blob.Properties.ContentType = AFS_BINARY_MODE;
                root_blob.UploadFromStream(root);
                root.Close();

            //            CloudBlockBlob blob = _rootPath.GetBlockBlobReference("a/");
            //           blob.Properties.ContentType = "application/octet-stream";
            //            Stream a = new MemoryStream();
            //            blob.UploadFromStream(a);
            //            a.Close();

                /* create root node */
                _rootNode = new AzureFileSystemNode(null, AFS_ROOT,
                    NodeType.DIRECTORY);

            }
            catch (WebException e)
            {
                System.Diagnostics.Trace.TraceError(
                    "An error occurs while intializing Azure file system. {0}",
                    e.Message);
            }

            _initialized = true;
            System.Diagnostics.Trace.TraceInformation(
                "Azure file system format complete!");
        }
Пример #7
0
        //----------------------------------------------------------------------
        // ToDo.UploadDataFileToBlobStorage
        //
        // Input   : fileContent - input stream of the file to be uploaded
        //           blobContainer - blob container in which the file stored
        // Return  : name of Blob file created in Azure blob storage
        // Purpose : Upload a local data file to Azure blob storage
        // Note    :
        //
        // * To upload a file to blob storage:
        //
        // CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobAddressUri);
        // blob.UploadFromStream(fileContent);
        //----------------------------------------------------------------------
        public static string UploadDataFileToBlobStorage(Stream fileContent, CloudBlobContainer blobContainer)
        {
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(ToDo.blobAddressUri);
            blob.UploadFromStream(fileContent);

            return ToDo.blobAddressUri;
        }
Пример #8
0
        //----------------------------------------------------------------------
        // ToDo.LoadFollowerTableFromBlob
        //
        // Input   : blobContainer - blob container which has data file we uploaded
        //           followerTable - empty follower table
        // Return  : Number of inserted entries (records) into follower table
        // Purpose : Load followerTable with follower entries which are composed from blob object
        // Note    :
        //
        // * To read a blob file line by line:
        //
        // StreamReader sr = new StreamReader(blobContainer.GetBlobReference(BlobAddressUri).OpenRead());
        // String line;
        // while ((line = sr.ReadLine()) != null)
        // {  ...
        //
        // * Format of the line of input data:
        //
        // user_id  \t  follower_id
        //
        // * To insert an entry to follower table:
        //
        // followerTable.AddFollowerEntry(new FollowerEntry(userID, followerID, partitionKey));
        // followerTable.SaveChanges();
        //----------------------------------------------------------------------
        public static int LoadFollowerTableFromBlob(CloudBlobContainer blobContainer, FollowerEntryDataSource followerTable)
        {
            int numFollowerEntries = 0;

            StreamReader sr = new StreamReader(blobContainer.GetBlockBlobReference(ToDo.blobAddressUri).OpenRead());
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                numFollowerEntries++;
                String [] split = line.Split(new Char [] {'\t'});
                int User = Convert.ToInt32(split[0]);
                int Follower = Convert.ToInt32(split[1]);
                // Partition Key = (User + Follower) % #(Worker Role Instances)
                // Users who are mutually friends to each other are contained in the same partition
                string PartitionKey = ((User + Follower) % ToDo.NumWorkerRoleInstances).ToString();
                followerTable.AddFollowerEntry(new FollowerEntry(User, Follower, PartitionKey));
                followerTable.SaveChages();
            }

            return numFollowerEntries;
        }