/// <summary>
        /// Check Existence of the specified identifier.
        /// </summary>
        /// <param name="identifier">The storage identifier.</param>
        /// <returns><c>true</c> if existed, <c>false</c> otherwise.</returns>
        public override bool Exists(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));

                if (!string.IsNullOrWhiteSpace(identifier.Container))
                {
                    var            container = blobClient.GetContainerReference(identifier.Container);
                    CloudBlockBlob blob      = null;

                    if (!string.IsNullOrEmpty(identifier.Identifier))
                    {
                        blob = container.GetBlockBlobReference(identifier.Identifier);
                    }

                    return(blob != null && blob.Exists());
                }
            }
            catch (Exception ex)
            {
                throw ex.Handle(identifier);
            }

            return(false);
        }
        /// <summary>
        /// Fetches the cloud meta. Returned object would only includes (md5, length, name, content type).
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns>Beyova.BinaryStorageMetaData.</returns>
        public override BinaryStorageMetaData FetchCloudMeta(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));
                identifier.Container.CheckEmptyString(nameof(identifier.Container));
                identifier.Identifier.CheckEmptyString(nameof(identifier.Identifier));

                var request = new GetObjectMetadataRequest
                {
                    BucketName = identifier.Container,
                    Key        = identifier.Identifier,
                };

                var blob   = blobClient.GetObjectMetadata(request);
                var result = new BinaryStorageMetaData(identifier)
                {
                    Hash        = blob.Metadata["x-amz-meta-md5hash"],
                    Length      = blob.ContentLength,
                    Name        = blob.Headers.ContentDisposition,
                    ContentType = blob.Headers.ContentType
                };

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle(identifier);
            }
        }
        /// <summary>
        /// Fetches the cloud meta. Returned object would only includes (md5, length, name, content type).
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns>Beyova.BinaryStorageMetaData.</returns>
        public override BinaryStorageMetaData FetchCloudMeta(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));
                identifier.Container.CheckEmptyString("identifier.Container");
                identifier.Identifier.CheckEmptyString("identifier.Identifier");

                var container = blobClient.GetContainerReference(identifier.Container);
                var blob      = container.GetBlockBlobReference(identifier.Identifier);
                blob.FetchAttributes();

                return(new BinaryStorageMetaData(identifier)
                {
                    Hash = blob.Properties.ContentMD5,
                    Length = blob.Properties.Length,
                    Name = blob.Properties.ContentDisposition,
                    ContentType = blob.Properties.ContentType
                });
            }
            catch (Exception ex)
            {
                throw ex.Handle(identifier);
            }
        }
        /// <summary>
        /// Check Existence of the specified identifier.
        /// </summary>
        /// <param name="identifier">The storage identifier.</param>
        /// <returns><c>true</c> if existed, <c>false</c> otherwise.</returns>
        public override bool Exists(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));

                if (!string.IsNullOrWhiteSpace(identifier.Container))
                {
                    using (var response = blobClient.GetObject(identifier.Container, identifier.Identifier))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("not exist"))
                {
                    return(false);
                }
                else
                {
                    throw ex.Handle(identifier);
                }
            }

            return(false);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryStorageIdentifier"/> class.
 /// </summary>
 /// <param name="identifier">The identifier.</param>
 public BinaryStorageIdentifier(BinaryStorageIdentifier identifier)
 {
     if (identifier != null)
     {
         Container  = identifier.Container;
         Identifier = identifier.Identifier;
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Deletes the BLOB.
        /// </summary>
        /// <param name="storageIdentifier">The storage identifier.</param>
        public override void DeleteBlob(BinaryStorageIdentifier storageIdentifier)
        {
            try
            {
                storageIdentifier.CheckNullObject(nameof(storageIdentifier));
                storageIdentifier.Container.CheckEmptyString(nameof(storageIdentifier.Container));
                storageIdentifier.Identifier.CheckEmptyString(nameof(storageIdentifier.Identifier));

                client.DeleteObject(storageIdentifier.Container, storageIdentifier.Identifier);
            }
            catch (Exception ex)
            {
                throw ex.Handle(storageIdentifier);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Check Existence of the specified identifier.
        /// </summary>
        /// <param name="identifier">The storage identifier.</param>
        /// <returns><c>true</c> if existed, <c>false</c> otherwise.</returns>
        public override bool Exists(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));

                if (!string.IsNullOrWhiteSpace(identifier.Container))
                {
                    return(client.DoesObjectExist(identifier.Container, identifier.Identifier));
                }
            }
            catch (Exception ex)
            {
                throw ex.Handle(identifier);
            }

            return(false);
        }
        /// <summary>
        /// Queries the BLOB.
        /// </summary>
        /// <param name="containerName">Name of the container.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="hash">The MD5.</param>
        /// <param name="length">The length.</param>
        /// <param name="limitCount">The limit count.</param>
        /// <returns>
        /// List&lt;BinaryStorageMetaBase&gt;.
        /// </returns>
        public override List <BinaryStorageMetaData> QueryBinaryBlobByContainer(string containerName, string contentType = null,
                                                                                CryptoKey hash = null, long?length = null, int limitCount = 10)
        {
            try
            {
                containerName.CheckEmptyString(nameof(containerName));

                var request = new ListObjectsRequest
                {
                    BucketName = containerName,
                };

                var s3ObjectList = blobClient.ListObjects(request).S3Objects;
                List <BinaryStorageMetaData> binaryStorageMetaBaseList = new List <BinaryStorageMetaData>();
                foreach (var s3Obj in s3ObjectList)
                {
                    var key = s3Obj.Key;
                    BinaryStorageIdentifier identifier = new BinaryStorageIdentifier
                    {
                        Container  = containerName,
                        Identifier = key
                    };
                    var meta = FetchCloudMeta(identifier);

                    if ((string.IsNullOrWhiteSpace(contentType) || meta.ContentType.Equals(contentType, StringComparison.OrdinalIgnoreCase)) &&
                        (string.IsNullOrWhiteSpace(hash) || meta.Hash.Equals(hash)) &&
                        (!length.HasValue || meta.Length == length))
                    {
                        binaryStorageMetaBaseList.Add(meta);
                    }

                    if (binaryStorageMetaBaseList.Count >= limitCount)
                    {
                        return(binaryStorageMetaBaseList);
                    }
                }

                return(binaryStorageMetaBaseList);
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { containerName, limitCount });
            }
        }
        /// <summary>
        /// Deletes the BLOB.
        /// </summary>
        /// <param name="storageIdentifier">The storage identifier.</param>
        public override void DeleteBlob(BinaryStorageIdentifier storageIdentifier)
        {
            try
            {
                storageIdentifier.CheckNullObject(nameof(storageIdentifier));
                storageIdentifier.Container.CheckEmptyString("storageIdentifier.Container");
                storageIdentifier.Identifier.CheckEmptyString("storageIdentifier.Identifier");

                if (Exists(storageIdentifier))
                {
                    var container = blobClient.GetContainerReference(storageIdentifier.Container);
                    var blob      = container.GetBlockBlobReference(storageIdentifier.Identifier);
                    blob?.Delete();
                }
            }
            catch (Exception ex)
            {
                throw ex.Handle(storageIdentifier);
            }
        }
        /// <summary>
        /// Deletes the BLOB.
        /// </summary>
        /// <param name="storageIdentifier">The storage identifier.</param>
        public override void DeleteBlob(BinaryStorageIdentifier storageIdentifier)
        {
            try
            {
                storageIdentifier.CheckNullObject(nameof(storageIdentifier));
                storageIdentifier.Container.CheckEmptyString(nameof(storageIdentifier.Container));
                storageIdentifier.Identifier.CheckEmptyString(nameof(storageIdentifier.Identifier));

                var deleteObjectRequest = new DeleteObjectRequest
                {
                    BucketName = storageIdentifier.Container,
                    Key        = storageIdentifier.Identifier
                };
                DeleteObjectResponse result = blobClient.DeleteObject(deleteObjectRequest);
            }
            catch (Exception ex)
            {
                throw ex.Handle(storageIdentifier);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Fetches the cloud meta. Returned object would only includes (md5, length, name, content type).
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns>Beyova.BinaryStorageMetaData.</returns>
        public override BinaryStorageMetaData FetchCloudMeta(BinaryStorageIdentifier identifier)
        {
            try
            {
                identifier.CheckNullObject(nameof(identifier));
                identifier.Container.CheckEmptyString(nameof(identifier.Container));
                identifier.Identifier.CheckEmptyString(nameof(identifier.Identifier));

                var meta   = client.GetObjectMetadata(identifier.Container, identifier.Identifier);
                var result = new BinaryStorageMetaData(identifier)
                {
                    Hash        = meta.ContentMd5,
                    Length      = meta.ContentLength,
                    Name        = meta.ContentDisposition,
                    ContentType = meta.ContentType
                };

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle(identifier);
            }
        }