Exemple #1
0
        /// <summary>
        /// Overwrites the object stored at the original path with the new object. Checks if the existing path exists, then calls PUT on the new path.
        /// </summary>
        /// <param name="originalPath">The original path.</param>
        /// <param name="newObject">The new object.</param>
        public void Overwrite(string originalPath, AzureCloudFile newObject)
        {
            // Check if the original path exists on the provider.
            if (!CheckBlobExists(originalPath))
            {
                throw new FileNotFoundException("The path supplied does not exist on the storage provider.",
                                                originalPath);
            }

            // Put the new object over the top of the old...
            Put(newObject);
        }
Exemple #2
0
        /// <summary>
        /// Delete the specified AzureCloudFile from the Azure container.
        /// </summary>
        /// <param name="o">The object to be deleted.</param>
        public void Delete(AzureCloudFile o)
        {
            string path = UriPathToString(o.Uri);

            if (path.StartsWith("/"))
            {
                path = path.Remove(0, 1);
            }

            CloudBlobContainer c = GetContainerReference(ContainerName);
            CloudBlob          b = c.GetBlobReference(path);

            if (b != null)
            {
                b.BeginDelete(new AsyncCallback(DeleteOperationCompleteCallback), o.Uri);
            }
            else
            {
                throw new ArgumentException("The container reference could not be retrieved from storage provider.", "o");
            }
        }
Exemple #3
0
 public AzureFileInfo(AzureCloudFile file)
 {
     _file = file;
 }
Exemple #4
0
        /// <summary>
        /// Retrieves the object from the storage provider
        /// </summary>
        /// <param name="path">the file/dir path in the FtpServer</param>
        /// <param name="isDirectory">whether path is a directory</param>
        /// <returns>AzureCloudFile</returns>
        /// <exception cref="FileNotFoundException">Throws a FileNotFoundException if the blob path is not found on the provider.</exception>
        public AzureCloudFile GetBlobInfo(string path, bool isDirectory)
        {
            // check parameter
            if (string.IsNullOrEmpty(path) || path[0] != '/')
            {
                return(null);
            }

            // get the info of root directory
            if ((path == "/") && isDirectory)
            {
                return new AzureCloudFile
                       {
                           Uri          = _container.Uri,
                           FtpPath      = path,
                           IsDirectory  = true,
                           Size         = 1,
                           LastModified = DateTime.Now
                       }
            }
            ;

            // convert to azure path
            string blobPath = path.ToAzurePath();

            var azureCloudFile = new AzureCloudFile();

            try
            {
                if (isDirectory)
                {
                    CloudBlobDirectory bDir = _container.GetDirectoryReference(blobPath);

                    // check whether directory exists
                    if (!bDir.ListBlobs().Any())
                    {
                        throw new StorageException(new RequestResult(), "directory is empty", new Exception());
                    }

                    azureCloudFile = new AzureCloudFile
                    {
                        Uri         = bDir.Uri,
                        FtpPath     = path,
                        IsDirectory = true,
                        // default value for size and modify time of directories
                        Size         = 1,
                        LastModified = DateTime.Now
                    };
                }
                else
                {
                    ICloudBlob b = _container.GetBlockBlobReference(blobPath);
                    b.FetchAttributes();
                    azureCloudFile = new AzureCloudFile
                    {
                        Uri          = b.Uri,
                        LastModified = b.Properties.LastModified.GetValueOrDefault(),
                        Size         = b.Properties.Length,
                        FtpPath      = path,
                        IsDirectory  = false
                    };
                }
            }
            catch (StorageException)
            {
                Trace.WriteLine($"Get blob {path} failed", "Error");

                return(null);
            }

            return(azureCloudFile);
        }
 public AzureFileInfo(AzureCloudFile file, AzureBlobStorageProvider provider)
 {
     _file = file;
     _provider = provider;
 }
Exemple #6
0
        /// <summary>
        /// Retrieves the object from the storage provider
        /// </summary>
        /// <param name="path">the file/dir path in the FtpServer</param>
        /// <param name="isDirectory">whether path is a directory</param>
        /// <returns>AzureCloudFile</returns>
        /// <exception cref="FileNotFoundException">Throws a FileNotFoundException if the blob path is not found on the provider.</exception>
        public AzureCloudFile GetBlobInfo(string path, bool isDirectory)
        {
            // check parameter
            if (path == null || path == "" || path[0] != '/')
            {
                return(null);
            }

            // get the info of root directory
            if ((path == "/") && isDirectory)
            {
                return new AzureCloudFile
                       {
                           Uri          = _container.Uri,
                           FtpPath      = path,
                           IsDirectory  = true,
                           Size         = 1,
                           LastModified = DateTime.Now
                       }
            }
            ;

            // convert to azure path
            string blobPath = path.ToAzurePath();

            var o = new AzureCloudFile();

            try
            {
                if (isDirectory)
                {
                    CloudBlobDirectory bDir = _container.GetDirectoryReference(blobPath);
                    // check whether directory exists
                    if (bDir.ListBlobs().Count() == 0)
                    {
                        throw new StorageException();
                    }
                    o = new AzureCloudFile
                    {
                        Uri         = bDir.Uri,
                        FtpPath     = path,
                        IsDirectory = true,
                        // default value for size and modify time of directories
                        Size         = 1,
                        LastModified = DateTime.Now
                    };
                }
                else
                {
                    CloudBlob b = _container.GetBlobReference(blobPath);
                    b.FetchAttributes();
                    o = new AzureCloudFile
                    {
                        Uri          = b.Uri,
                        LastModified = b.Properties.LastModified.Value.DateTime,
                        Size         = b.Properties.Length,
                        FtpPath      = path,
                        IsDirectory  = false
                    };
                }
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Get blob {0} failed", path), "Error");

                return(null);
            }

            return(o);
        }
        public bool Put(string sPath, IFile oFile)
        {
            var f = new AzureCloudFile
                        {
                            Uri = new Uri(sPath, UriKind.RelativeOrAbsolute),
                            Data = oFile.File.ToArray(),
                            Size = oFile.File.Length
                        };

            _provider.Put(f);
            return true;
        }
        public void Put(AzureCloudFile o)
        {
            if (o.Data == null)
                throw new ArgumentNullException("o", "AzureCloudFile cannot be null.");

            if (o.Uri == null)
                throw new ArgumentException("Parameter 'Uri' of argument 'o' cannot be null.");

            string path = o.Uri.ToString();

            if (path.StartsWith(@"/"))
                path = path.Remove(0, 1);

            if (path.StartsWith(@"\\"))
                path = path.Remove(0, 1);

            if (path.StartsWith(@"\"))
                path = path.Remove(0, 1);

            path = path.Replace(@"\\", @"\");

            CloudBlobContainer container = _blobClient.GetContainerReference(ContainerName);
            container.CreateIfNotExist();

            BlobContainerPermissions perms = container.GetPermissions();
            if (perms.PublicAccess != BlobContainerPublicAccessType.Container)
            {
                perms.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(perms);
            }

            String uniqueName = path;
            CloudBlob blob = container.GetBlobReference(uniqueName);

            AsyncCallback callback = PutOperationCompleteCallback;
            blob.BeginUploadFromStream(new MemoryStream(o.Data), callback, o.Uri);

            // Uncomment for synchronous upload
            //blob.UploadFromStream(new System.IO.MemoryStream(o.Data));
        }
        public void Overwrite(string originalPath, AzureCloudFile newObject)
        {
            if (!CheckBlobExists(originalPath))
            {
                throw new FileNotFoundException("The path supplied does not exist on the storage provider.",
                                                originalPath);
            }

            Put(newObject);
        }
        public AzureCloudFile Get(string path, bool downloadData)
        {
            var u = new Uri(path, UriKind.RelativeOrAbsolute);
            string blobPath = UriPathToString(u);

            if (blobPath.StartsWith(@"/"))
                blobPath = blobPath.Remove(0, 1);

            blobPath = RemoveContainerName(blobPath);

            var o = new AzureCloudFile();
            CloudBlobContainer c = GetContainerReference(ContainerName);

            CloudBlob b = null;

            try
            {
                b = c.GetBlobReference(blobPath);
                b.FetchAttributes();
                o = new AzureCloudFile
                        {
                            Meta = b.Metadata,
                            StorageOperationResult = StorageOperationResult.Completed,
                            Uri = new Uri(blobPath, UriKind.RelativeOrAbsolute),
                            LastModified = b.Properties.LastModifiedUtc,
                            ContentType = b.Properties.ContentType,
                            Size = b.Properties.Length
                        };

                o.Meta.Add("ContentType", b.Properties.ContentType);
            }
            catch (StorageClientException ex)
            {
                if (ex.ErrorCode == StorageErrorCode.BlobNotFound)
                {
                    throw new FileNotFoundException(
                        "The storage provider was unable to locate the object identified by the given URI.",
                        u.ToString());
                }

                if (ex.ErrorCode == StorageErrorCode.ResourceNotFound)
                {
                    return null;
                }
            }

            // TODO: Implement asynchronous calls for this
            try
            {
                if (downloadData && b != null)
                {
                    byte[] data = b.DownloadByteArray();
                    o.Data = data;
                }
            }

            catch (TimeoutException)
            {
                if (RetryOnTimeout)
                {
                    Get(blobPath, downloadData);
                    // TODO: Implement retry attempt limitation
                }
                else
                {
                    throw;
                }
            }

            return o;
        }
        public void Delete(AzureCloudFile o)
        {
            string path = UriPathToString(o.Uri);
            if (path.StartsWith("/"))
                path = path.Remove(0, 1);

            CloudBlobContainer c = GetContainerReference(ContainerName);
            CloudBlob b = c.GetBlobReference(path);
            if (b != null)
                b.BeginDelete(new AsyncCallback(DeleteOperationCompleteCallback), o.Uri);
            else
                throw new ArgumentException("The container reference could not be retrieved from storage provider.", "o");
        }
        /// <summary>
        /// Retrieves the object from the storage provider
        /// </summary>
        /// <param name="path">the file/dir path in the FtpServer</param>
        /// <param name="isDirectory">whether path is a directory</param>
        /// <returns>AzureCloudFile</returns>
        /// <exception cref="FileNotFoundException">Throws a FileNotFoundException if the blob path is not found on the provider.</exception>
        public AzureCloudFile GetBlobInfo(string path, bool isDirectory)
        {
            // check parameter
            if (path == null || path == "" || path[0] != '/')
                return null;

            // get the info of root directory
            if ((path == "/") && isDirectory)
                return new AzureCloudFile
                            {
                                Uri = _container.Uri,
                                FtpPath = path,
                                IsDirectory = true,
                                Size = 1,
                                LastModified = DateTime.Now
                            };

            // convert to azure path
            string blobPath = path.ToAzurePath();

            var o = new AzureCloudFile();

            try
            {
                if (isDirectory)
                {
                    CloudBlobDirectory bDir = _container.GetDirectoryReference(blobPath);
                    // check whether directory exists
                    if (bDir.ListBlobs().Count() == 0)
                        throw new StorageException(new RequestResult(), "directory is empty", new Exception());
                    o = new AzureCloudFile
                            {
                                Uri = bDir.Uri,
                                FtpPath = path,
                                IsDirectory = true,
                                // default value for size and modify time of directories
                                Size = 1,
                                LastModified = DateTime.Now
                            };
                }
                else
                {
                    ICloudBlob b = _container.GetBlockBlobReference(blobPath);
                    b.FetchAttributes();
                    o = new AzureCloudFile
                            {
                                Uri = b.Uri,
                                LastModified = b.Properties.LastModified.GetValueOrDefault(),
                                Size = b.Properties.Length,
                                FtpPath = path,
                                IsDirectory = false
                            };
                }

            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Get blob {0} failed", path),"Error");
                return null;
            }

            return o;
        }
        /// <summary>
        /// Overwrites the object stored at the original path with the new object. Checks if the existing path exists, then calls PUT on the new path.
        /// </summary>
        /// <param name="originalPath">The original path.</param>
        /// <param name="newObject">The new object.</param>
        public void Overwrite(string originalPath, AzureCloudFile newObject) {
            // Check if the original path exists on the provider.
            if (!CheckBlobExists(originalPath)) {
                throw new FileNotFoundException("The path supplied does not exist on the storage provider.",
                                                originalPath);
            }

            // Put the new object over the top of the old...
            Put(newObject);
        }
        /// <summary>
        /// Retrieves the object from the storage provider
        /// </summary>
        /// <param name="path">The fully qualified OR relative URI to the object to be retrieved</param>
        /// <param name="downloadData">Boolean indicating whether to download the contents of the file to the Data property or not</param>
        /// <returns>AzureCloudFile</returns>
        /// <exception cref="FileNotFoundException">Throws a FileNotFoundException if the URI is not found on the provider.</exception>
        public AzureCloudFile Get(string path, bool downloadData) {
            UriKind kind = UriKind.RelativeOrAbsolute;
            CloudBlobContainer parentContainer = _blobClient.GetContainerReference(ContainerName);
            Uri u = new Uri(parentContainer.Uri.ToString() + path, kind);
            string blobPath = UriPathToString(u);

            if (blobPath.StartsWith(@"/"))
                blobPath = blobPath.Remove(0, 1);

            //Uri uri = new Uri(blobPath);
            //blobPath = RemoveContainerName(blobPath);
            var o = new AzureCloudFile();
            ICloudBlob b = null;
            try {
                b = _blobClient.GetBlobReferenceFromServer(u);
                
                b.FetchAttributes();
                o = new AzureCloudFile {
                    Meta = b.Metadata,
                    StorageOperationResult = StorageOperationResult.Completed,
                    Uri = new Uri(blobPath, UriKind.RelativeOrAbsolute),
                    LastModified = b.Properties.LastModified.Value.UtcDateTime,
                    ContentType = b.Properties.ContentType,
                    Size = b.Properties.Length
                };

                o.Meta.Add("ContentType", b.Properties.ContentType);
            } catch {
                return null;
            }

            // Try to download the data for the blob, if requested
            // TODO: Implement asynchronous calls for this
            try {
                if (downloadData && b != null) {
                    MemoryStream datastream = new MemoryStream();
                    b.DownloadToStream(datastream);
                    byte[] data = datastream.ToArray();
                    o.Data = data;
                }
            } catch (TimeoutException) {
                if (RetryOnTimeout) {
                    Get(blobPath, downloadData); // NOTE: Infinite retries, what fun! :)
                    // TODO: Implement retry attempt limitation
                } else {
                    throw;
                }
            }

            return o;
        }
Exemple #15
0
        /// <summary>
        /// Retrieves the object from the storage provider
        /// </summary>
        /// <param name="path">The fully qualified OR relative URI to the object to be retrieved</param>
        /// <param name="downloadData">Boolean indicating whether to download the contents of the file to the Data property or not</param>
        /// <returns>AzureCloudFile</returns>
        /// <exception cref="FileNotFoundException">Throws a FileNotFoundException if the URI is not found on the provider.</exception>
        public AzureCloudFile Get(string path, bool downloadData)
        {
            var    u        = new Uri(path, UriKind.RelativeOrAbsolute);
            string blobPath = UriPathToString(u);

            if (blobPath.StartsWith(@"/"))
            {
                blobPath = blobPath.Remove(0, 1);
            }

            blobPath = RemoveContainerName(blobPath);

            var o = new AzureCloudFile();
            CloudBlobContainer c = GetContainerReference(ContainerName);

            CloudBlob b = null;

            try
            {
                b = c.GetBlobReference(blobPath);
                b.FetchAttributes();
                o = new AzureCloudFile
                {
                    Meta = b.Metadata,
                    StorageOperationResult = StorageOperationResult.Completed,
                    Uri          = new Uri(blobPath, UriKind.RelativeOrAbsolute),
                    LastModified = b.Properties.LastModifiedUtc,
                    ContentType  = b.Properties.ContentType,
                    Size         = b.Properties.Length
                };

                o.Meta.Add("ContentType", b.Properties.ContentType);
            }
            catch (StorageClientException ex)
            {
                if (ex.ErrorCode == StorageErrorCode.BlobNotFound)
                {
                    throw new FileNotFoundException(
                              "The storage provider was unable to locate the object identified by the given URI.",
                              u.ToString());
                }

                if (ex.ErrorCode == StorageErrorCode.ResourceNotFound)
                {
                    return(null);
                }
            }

            // Try to download the data for the blob, if requested
            // TODO: Implement asynchronous calls for this
            try
            {
                if (downloadData && b != null)
                {
                    byte[] data = b.DownloadByteArray();
                    o.Data = data;
                }
            }

            catch (TimeoutException)
            {
                if (RetryOnTimeout)
                {
                    Get(blobPath, downloadData); // NOTE: Infinite retries, what fun! :)
                    // TODO: Implement retry attempt limitation
                }
                else
                {
                    throw;
                }
            }

            return(o);
        }
Exemple #16
0
        /// <summary>
        /// Puts the specified object onto the cloud storage provider.
        /// </summary>
        /// <param name="o">The object to store.</param>
        public void Put(AzureCloudFile o)
        {
            if (o.Data == null)
            {
                throw new ArgumentNullException("o", "AzureCloudFile cannot be null.");
            }

            if (o.Uri == null)
            {
                throw new ArgumentException("Parameter 'Uri' of argument 'o' cannot be null.");
            }

            string path = o.Uri.ToString();

            if (path.StartsWith(@"/"))
            {
                path = path.Remove(0, 1);
            }

            if (path.StartsWith(@"\\"))
            {
                path = path.Remove(0, 1);
            }

            if (path.StartsWith(@"\"))
            {
                path = path.Remove(0, 1);
            }

            // Remove double back slashes from anywhere in the path
            path = path.Replace(@"\\", @"\");

            CloudBlobContainer container = _blobClient.GetContainerReference(ContainerName);

            container.CreateIfNotExist();

            // Set permissions on the container
            BlobContainerPermissions perms = container.GetPermissions();

            if (perms.PublicAccess != BlobContainerPublicAccessType.Container)
            {
                perms.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(perms);
            }


            // Create a reference for the filename
            String uniqueName = path;

            blob = container.GetBlobReference(uniqueName);

            // Create a new AsyncCallback instance
            AsyncCallback callback = PutOperationCompleteCallback;



            blob.BeginUploadFromStream(new MemoryStream(o.Data), callback, o.Uri);


            // Uncomment for synchronous upload
            // blob.UploadFromStream(new System.IO.MemoryStream(o.Data));
        }