Esempio n. 1
0
        internal bool ExistsWithBucketCheck(out bool bucketExists)
        {
            bucketExists = true;
            try
            {
                var request = new HeadObjectRequest
                {
                    NamespaceName = namespaceName,
                    BucketName    = bucket,
                    ObjectName    = ObjectStorageHelper.EncodeKey(key)
                };

                // If the object doesn't exist then a "NotFound" will be thrown
                client.HeadObject(request);
                return(true);
            }
            catch (WebException we)
            {
                if (we.Status.Equals(WebExceptionStatus.ProtocolError) && ((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    bucketExists = false;
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Copies the file from the local file system to ObjectStorage.
        /// If the file already exists in ObjectStorage and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="srcFileName">Location of the file on the local file system to copy.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists in ObjectStorage and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        public ObjectStorageFileInfo CopyFromLocal(string srcFileName, bool overwrite)
        {
            if (!overwrite)
            {
                if (Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            var putObjectRequest = new PutObjectRequest
            {
                NamespaceName = namespaceName,
                BucketName    = bucket,
                ObjectName    = ObjectStorageHelper.EncodeKey(key)
            };

            using (var stream = File.Open(srcFileName, FileMode.Open))
            {
                putObjectRequest.UploadPartBody = stream;
                var updateRes = client.PutObject(putObjectRequest);
            }

            return(this);
        }
Esempio n. 3
0
        private List <string> GetOciPrefixs(string prefix, SearchOption searchOption, string nextStartWith = "")
        {
            var res = new List <string>();

            var listObjectsRequest = new ListObjectsRequest()
            {
                NamespaceName = NamespaceName,
                BucketName    = BucketName,
                Start         = nextStartWith,
                Delimiter     = "/",
                Prefix        = ObjectStorageHelper.EncodeKey(prefix),
                Fields        = new List <string> {
                    "name"
                },
                Limit = 1
            };

            var objects = Client.ListObjects(listObjectsRequest);

            res.AddRange(objects.ListObjects.Prefixes);

            if (!string.IsNullOrEmpty(objects.ListObjects.NextStartWith))
            {
                res.AddRange(GetOciPrefixs(ObjectStorageHelper.DecodeKey(prefix), searchOption, objects.ListObjects.NextStartWith));
            }

            if (objects.ListObjects.Prefixes != null && objects.ListObjects.Prefixes.Count > 0)
            {
                // all sub prefix
                if (searchOption == SearchOption.AllDirectories)
                {
                    foreach (var pre in objects.ListObjects.Prefixes)
                    {
                        res.AddRange(GetOciPrefixs(pre, searchOption));
                    }
                }
                else
                {
                    var newPrefix = objects.ListObjects.Prefixes.Where(p => p == ObjectStorageHelper.EncodeKey(prefix) + "/");
                    if (newPrefix != null && newPrefix.Count() > 0)
                    {
                        foreach (var pre in newPrefix)
                        {
                            res.AddRange(GetOciPrefixs(pre, searchOption));
                        }
                    }
                }
            }

            return(res);
        }
Esempio n. 4
0
        /// <summary>
        /// Copies from ObjectStorage to the local file system.
        /// If the file already exists on the local file system and overwrite is set to false than an ArgumentException is thrown.
        /// </summary>
        /// <param name="destFileName">The path where to copy the file to.</param>
        /// <param name="overwrite">Determines whether the file can be overwritten.</param>
        /// <exception cref="T:System.IO.IOException">If the file already exists locally and overwrite is set to false.</exception>
        /// <exception cref="T:System.Net.WebException"></exception>
        /// <returns>FileInfo for the local file where file is copied to.</returns>
        public FileInfo CopyToLocal(string destFileName, bool overwrite)
        {
            if (!overwrite)
            {
                if (new FileInfo(destFileName).Exists)
                {
                    throw new IOException("File already exists");
                }
            }

            var getObjectRequest = new GetObjectRequest
            {
                NamespaceName = namespaceName,
                BucketName    = bucket,
                ObjectName    = ObjectStorageHelper.EncodeKey(key)
            };

            client.DownloadObject(getObjectRequest, destFileName);

            return(new FileInfo(destFileName));
        }
Esempio n. 5
0
 /// <summary>
 /// Deletes the from ObjectStorage.
 /// </summary>
 /// <exception cref="T:System.Net.WebException"></exception>
 public void Delete()
 {
     if (Exists)
     {
         var deleteObjectRequest = new DeleteObjectRequest
         {
             NamespaceName = namespaceName,
             BucketName    = bucket,
             ObjectName    = ObjectStorageHelper.EncodeKey(key)
         };
         try
         {
             client.DeleteObject(deleteObjectRequest);
         }
         catch (WebException we)
         {
             if (!(we.Status.Equals(WebExceptionStatus.ProtocolError) && ((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.NotFound))
             {
                 throw;
             }
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Deletes all the files in this directory as well as this directory.  If recursive is set to true then all sub directories will be
        /// deleted as well.
        /// </summary>
        /// <exception cref="T:System.Net.WebException"></exception>
        public void Delete(bool recursive)
        {
            if (String.IsNullOrEmpty(BucketName))
            {
                throw new NotSupportedException();
            }

            if (recursive)
            {
                ListObjectsRequest listRequest = new ListObjectsRequest
                {
                    NamespaceName = NamespaceName,
                    BucketName    = BucketName,
                    Prefix        = ObjectStorageHelper.EncodeKey(ObjectKey)
                };

                DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest
                {
                    NamespaceName = NamespaceName,
                    BucketName    = BucketName,
                    Objects       = new List <TargetDelete>()
                };

                ListObjectsResponse listResponse;
                do
                {
                    listResponse = Client.ListObjects(listRequest);

                    foreach (var obj in listResponse.ListObjects.Objects)
                    {
                        deleteRequest.Objects.Add(new TargetDelete {
                            ObjectName = obj.Name
                        });
                        if (deleteRequest.Objects.Count >= MULTIPLE_OBJECT_DELETE_LIMIT)
                        {
                            Client.DeleteObjects(deleteRequest);
                            deleteRequest.Objects.Clear();
                        }
                    }

                    if (string.IsNullOrEmpty(listResponse.ListObjects.NextStartWith))
                    {
                        break;
                    }
                    else
                    {
                        listRequest.Start = listResponse.ListObjects.NextStartWith;
                    }
                } while (true);

                if (deleteRequest.Objects.Count > 0)
                {
                    Client.DeleteObjects(deleteRequest);
                }
            }

            if (String.IsNullOrEmpty(ObjectKey) && Exists)
            {
                var request = new DeleteBucketRequest {
                    NamespaceName = NamespaceName, BucketName = BucketName
                };
                Client.DeleteBucket(request);
            }
            else
            {
                if (!EnumerateFileSystemInfos().GetEnumerator().MoveNext() && Exists)
                {
                    var request = new DeleteObjectRequest
                    {
                        NamespaceName = NamespaceName,
                        BucketName    = BucketName,
                        ObjectName    = ObjectStorageHelper.EncodeKey(ObjectKey)
                    };

                    Client.DeleteObject(request);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Get the object in the bucket.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="searchOption"></param>
        /// <param name="maxCount"></param>
        /// <param name="nextStartWith"></param>
        /// <returns></returns>
        private List <ObjectSummary> GetOciObjects(string prefix, SearchOption searchOption, int maxCount, string nextStartWith = "")
        {
            var res = new List <ObjectSummary>();

            if (maxCount == -1)
            {
                maxCount = GET_OBJECT_LIMIT;
            }

            string delimiter = "";
            var    prefixReg = ObjectStorageHelper.EncodeKey(prefix);

            if (prefixReg.Contains("/"))
            {
                delimiter = "/";
            }

            var listObjectsRequest = new ListObjectsRequest()
            {
                NamespaceName = NamespaceName,
                BucketName    = BucketName,
                Start         = nextStartWith,
                Delimiter     = delimiter,
                Prefix        = prefixReg,
                Fields        = new List <string> {
                    "name", "size", "timeCreated", "md5"
                },
                Limit = maxCount
            };

            var objects = Client.ListObjects(listObjectsRequest);

            if (searchOption == SearchOption.TopDirectoryOnly)
            {
                foreach (var o in objects.ListObjects.Objects)
                {
                    if (CheckAddObject(o, prefixReg, searchOption))
                    {
                        res.Add(o);
                    }
                }
            }
            else
            {
                foreach (var o in objects.ListObjects.Objects)
                {
                    if (CheckAddObject(o, prefixReg, searchOption))
                    {
                        res.Add(o);
                    }
                }
            }

            NextPageId = objects.ListObjects.NextStartWith;
            // next check
            if (!string.IsNullOrEmpty(objects.ListObjects.NextStartWith))
            {
                if (maxCount == -1)
                {
                    res.AddRange(GetOciObjects(ObjectStorageHelper.DecodeKey(prefix), searchOption, maxCount, objects.ListObjects.NextStartWith));
                }
            }

            if (objects.ListObjects.Prefixes != null && objects.ListObjects.Prefixes.Count > 0)
            {
                // all sub prefix
                if (searchOption == SearchOption.AllDirectories)
                {
                    foreach (var pre in objects.ListObjects.Prefixes)
                    {
                        res.AddRange(GetOciObjects(pre, searchOption, maxCount));
                    }
                }
                else
                {
                    var newPrefix = objects.ListObjects.Prefixes.Where(p => p == prefixReg + "/");
                    if (newPrefix != null && newPrefix.Count() > 0)
                    {
                        foreach (var pre in newPrefix)
                        {
                            res.AddRange(GetOciObjects(pre, searchOption, maxCount));
                        }
                    }
                }
            }

            return(res);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns the directory in the s-bucket to the specified pattern
        /// </summary>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public IEnumerable <ObjectStorageDirectoryInfo> EnumerateDirectories(string searchPattern, SearchOption searchOption)
        {
            IEnumerable <ObjectStorageDirectoryInfo> folders = null;

            if (String.IsNullOrEmpty(BucketName))
            {
                var request = new ListBucketsRequest();
                folders = Client.ListBuckets(request).Items
                          .ConvertAll(bucket => new ObjectStorageDirectoryInfo(Client, NamespaceName, bucket.Name, ""));
            }
            else
            {
                var prefixs = GetOciPrefixs(ObjectKey, searchOption);

                folders = prefixs.ToList()
                          .ConvertAll(f => new ObjectStorageDirectoryInfo(Client, NamespaceName, BucketName, ObjectStorageHelper.DecodeKey(f)));
            }

            //filter based on search pattern
            var regEx = WildcardToRegex(searchPattern);

            folders = folders.Where(f => Regex.IsMatch(f.Name, regEx, RegexOptions.IgnoreCase));
            return(folders);
        }
Esempio n. 9
0
        /// <summary>
        /// returns files in a directory according to the specified search pattern
        /// </summary>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <param name="maxCount"></param>
        /// <param name="nextPageId"></param>
        /// <returns></returns>
        public IEnumerable <ObjectStorageFileInfo> EnumerateFiles(string searchPattern, SearchOption searchOption, int maxCount, string nextPageId)
        {
            IEnumerable <ObjectStorageFileInfo> files = null;

            if (String.IsNullOrEmpty(BucketName))
            {
                files = new List <ObjectStorageFileInfo>();
            }
            else
            {
                var metaFiles = GetOciObjects(ObjectKey, searchOption, maxCount, nextPageId).Where(f => !f.Name.EndsWith("/")).ToList();

                files = new EnumerableConverter <ObjectSummary, ObjectStorageFileInfo>
                            (metaFiles, o => new ObjectStorageFileInfo(Client, NamespaceName, BucketName, ObjectStorageHelper.DecodeKey(o.Name)));
            }

            var regEx = WildcardToRegex(searchPattern);

            files = files.Where(o => Regex.IsMatch(o.Name, regEx, RegexOptions.IgnoreCase));
            return(files);
        }
Esempio n. 10
0
        internal bool ExistsWithBucketCheck(out bool bucketExists)
        {
            bucketExists = true;
            try
            {
                if (String.IsNullOrEmpty(BucketName))
                {
                    return(true);
                }
                else if (String.IsNullOrEmpty(ObjectKey))
                {
                    var getBucketRequest = new HeadBucketRequest
                    {
                        NamespaceName = NamespaceName,
                        BucketName    = BucketName
                    };

                    try
                    {
                        Client.HeadBucket(getBucketRequest);
                        return(true);
                    }
                    catch (WebException we)
                    {
                        if (we.Status.Equals(WebExceptionStatus.ProtocolError) && ((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.NotFound)
                        {
                            return(false);
                        }
                        throw;
                    }
                }
                else
                {
                    var request = new ListObjectsRequest()
                    {
                        NamespaceName = NamespaceName,
                        BucketName    = BucketName,
                        Delimiter     = "/",
                        Prefix        = ObjectStorageHelper.EncodeKey(ObjectKey),
                        Limit         = 1
                    };
                    var response = Client.ListObjects(request);
                    return(response.ListObjects.Objects.Count > 0 || response.ListObjects.Prefixes.Count > 0);
                }
            }
            catch (WebException we)
            {
                if (we.Status.Equals(WebExceptionStatus.ProtocolError) && ((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }
            catch (Exception e)
            {
                if (string.Equals(e.Message, "NoSuchBucket"))
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }