Exemplo n.º 1
0
        public StorageTreeNode GetTreeNode(string strPath)
        {
            StorageTreeNode oNode = null;

            try
            {
                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    string strPrefix = GetDirPath(strPath);
                    Amazon.S3.Model.ListObjectsRequest oRequest = new Amazon.S3.Model.ListObjectsRequest();
                    oRequest.WithBucketName(m_strBucketName).WithPrefix(strPrefix);

                    using (Amazon.S3.Model.ListObjectsResponse oResponse = oS3Client.ListObjects(oRequest))
                    {
                        oNode = new StorageTreeNode(strPrefix.Substring(0, strPrefix.Length - 1), true);
                        foreach (Amazon.S3.Model.S3Object entry in oResponse.S3Objects)
                        {
                            AddNodeRecursive(oNode, entry.Key.Substring(strPrefix.Length));
                        }
                    }
                }
            }
            catch
            {
            }

            return(oNode);
        }
Exemplo n.º 2
0
        public IEnumerable <string> GetContainerNames()
        {
            const char delimiter = '/';

            var request = new Amazon.S3.Model.ListObjectsRequest {
                BucketName = ContentStorageBucketName, Delimiter = delimiter.ToString()
            };

            while (true)
            {
                var response = _amazonS3.ListObjectsAsync(request).Result;

                foreach (var commonPrefix in response.CommonPrefixes)
                {
                    yield return(commonPrefix.TrimEnd(delimiter));
                }

                if (!response.IsTruncated)
                {
                    break;
                }

                request.Marker = response.NextMarker;
            }
        }
Exemplo n.º 3
0
        public List <string> ListItems(string bucketName, string serverFolder, int?maxItems = null)
        {
            //ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingNetSDK.html

            List <string> listRest = new List <string>();
            int           count    = 0;

            var region = Amazon.RegionEndpoint.GetBySystemName(this.Credential.Region);

            using (var client = new Amazon.S3.AmazonS3Client(this.Credential.AcesssKey, this.Credential.SecretKey, region))
            {
                var request = new Amazon.S3.Model.ListObjectsRequest
                {
                    BucketName = bucketName,
                    MaxKeys    = 10,
                    Prefix     = serverFolder
                };

                do
                {
                    var response = client.ListObjects(request);

                    // Process response
                    foreach (Amazon.S3.Model.S3Object entry in response.S3Objects)
                    {
                        if (entry.Key == serverFolder || entry.Key == string.Format("{0}/", serverFolder) || entry.Key == string.Format("/{0}", serverFolder))
                        {
                            continue; //Folder
                        }
                        count++;

                        System.Diagnostics.Debug.WriteLine("AwsS3 -- key = {0} size = {1} / {2} items read", entry.Key, entry.Size.ToString("#,##0"), count.ToString("#,##0"));
                        listRest.Add(entry.Key);
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        request.Marker = response.NextMarker;
                    }
                    else
                    {
                        request = null;
                    }

                    if (maxItems.HasValue && count >= maxItems.Value)
                    {
                        break;
                    }
                } while (request != null);
            }

            return(listRest);
        }
Exemplo n.º 4
0
        public ErrorTypes RemovePath(string strPath)
        {
            ErrorTypes eResult = ErrorTypes.StorageRemoveDir;

            try
            {
                string strDirKey = GetDirPath(strPath);

                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    Amazon.S3.Model.ListObjectsRequest oListObjectsRequest = new Amazon.S3.Model.ListObjectsRequest();
                    oListObjectsRequest.WithBucketName(m_strBucketName).WithPrefix(strDirKey);

                    using (Amazon.S3.Model.ListObjectsResponse oListObjectsResponse = oS3Client.ListObjects(oListObjectsRequest))
                    {
                        int nDeletedObjectCount   = 0;
                        int nObjectsToDeleteCount = oListObjectsResponse.S3Objects.Count;
                        if (nObjectsToDeleteCount > 0)
                        {
                            Amazon.S3.Model.DeleteObjectsRequest oDeleteObjectsRequest = new Amazon.S3.Model.DeleteObjectsRequest();
                            oDeleteObjectsRequest.WithBucketName(m_strBucketName);

                            foreach (Amazon.S3.Model.S3Object oS3Obj in oListObjectsResponse.S3Objects)
                            {
                                oDeleteObjectsRequest.AddKey(oS3Obj.Key);
                            }

                            using (Amazon.S3.Model.DeleteObjectsResponse oDeleteObjectsResponse = oS3Client.DeleteObjects(oDeleteObjectsRequest))
                            {
                                nDeletedObjectCount = oDeleteObjectsResponse.DeletedObjects.Count;
                            }
                        }

                        if (nObjectsToDeleteCount == nDeletedObjectCount)
                        {
                            eResult = ErrorTypes.NoError;
                        }
                    }
                }
            }
            catch
            {
            }

            return(eResult);
        }
Exemplo n.º 5
0
        public void GetTreeNodeBegin(string strPath, AsyncCallback cb, object oParam)
        {
            try
            {
                m_oGetTreeNode             = new TransportClass(cb, oParam);
                m_oGetTreeNode.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion);
                m_oGetTreeNode.m_strPrefix = GetDirPath(strPath);
                Amazon.S3.Model.ListObjectsRequest oRequest = new Amazon.S3.Model.ListObjectsRequest();
                oRequest.WithBucketName(m_strBucketName).WithPrefix(m_oGetTreeNode.m_strPrefix);

                m_oGetTreeNode.m_oS3Client.BeginListObjects(oRequest, m_oGetTreeNode.m_fCallback, m_oGetTreeNode.m_oParam);
            }
            catch
            {
                m_oGetTreeNode.m_eError = ErrorTypes.StorageGetInfo;
                m_oGetTreeNode.DisposeAndCallback();
            }
        }
Exemplo n.º 6
0
        public void RemovePathBegin(string strPath, AsyncCallback cb, object oParam)
        {
            try
            {
                m_oRemoveDirectory             = new TransportClass(cb, oParam);
                m_oRemoveDirectory.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion);

                string strDirKey = GetDirPath(strPath);
                Amazon.S3.Model.ListObjectsRequest oListObjectsRequest = new Amazon.S3.Model.ListObjectsRequest()
                                                                         .WithBucketName(m_strBucketName).WithPrefix(strDirKey);

                m_oRemoveDirectory.m_oS3Client.BeginListObjects(oListObjectsRequest, EndCallbackGetListObjects, m_oRemoveDirectory);
            }
            catch
            {
                m_oRemoveDirectory.m_eError = ErrorTypes.StorageRemoveDir;
                m_oRemoveDirectory.DisposeAndCallback();
            }
        }
Exemplo n.º 7
0
        public void RemovePathBegin(string strPath, AsyncCallback cb, object oParam)
        {
            try
            {
                m_oRemoveDirectory = new TransportClass(cb, oParam);
                m_oRemoveDirectory.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion);

                string strDirKey = GetDirPath(strPath);
                Amazon.S3.Model.ListObjectsRequest oListObjectsRequest = new Amazon.S3.Model.ListObjectsRequest()
                    .WithBucketName(m_strBucketName).WithPrefix(strDirKey);

                m_oRemoveDirectory.m_oS3Client.BeginListObjects(oListObjectsRequest, EndCallbackGetListObjects, m_oRemoveDirectory);
            }
            catch
            {
                m_oRemoveDirectory.m_eError = ErrorTypes.StorageRemoveDir;
                m_oRemoveDirectory.DisposeAndCallback();
            }
        }
Exemplo n.º 8
0
        public ErrorTypes RemovePath(string strPath)
        {
            ErrorTypes eResult = ErrorTypes.StorageRemoveDir;
            try
            {
                string strDirKey = GetDirPath(strPath);

                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    Amazon.S3.Model.ListObjectsRequest oListObjectsRequest = new Amazon.S3.Model.ListObjectsRequest();
                    oListObjectsRequest.WithBucketName(m_strBucketName).WithPrefix(strDirKey);

                    using (Amazon.S3.Model.ListObjectsResponse oListObjectsResponse = oS3Client.ListObjects(oListObjectsRequest))
                    {
                        int nDeletedObjectCount = 0;
                        int nObjectsToDeleteCount = oListObjectsResponse.S3Objects.Count;
                        if (nObjectsToDeleteCount > 0)
                        {
                            Amazon.S3.Model.DeleteObjectsRequest oDeleteObjectsRequest = new Amazon.S3.Model.DeleteObjectsRequest();
                            oDeleteObjectsRequest.WithBucketName(m_strBucketName);

                            foreach (Amazon.S3.Model.S3Object oS3Obj in oListObjectsResponse.S3Objects)
                            {
                                oDeleteObjectsRequest.AddKey(oS3Obj.Key);
                            }

                            using (Amazon.S3.Model.DeleteObjectsResponse oDeleteObjectsResponse = oS3Client.DeleteObjects(oDeleteObjectsRequest))
                            {
                                nDeletedObjectCount = oDeleteObjectsResponse.DeletedObjects.Count;
                            }
                        }

                        if (nObjectsToDeleteCount == nDeletedObjectCount)
                            eResult = ErrorTypes.NoError;
                    }
                }
            }
            catch
            {
            }

            return eResult;
        }
Exemplo n.º 9
0
        public void GetTreeNodeBegin(string strPath, AsyncCallback cb, object oParam)
        {
            try
            {
                m_oGetTreeNode = new TransportClass(cb, oParam);
                m_oGetTreeNode.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion);
                m_oGetTreeNode.m_strPrefix = GetDirPath(strPath);
                Amazon.S3.Model.ListObjectsRequest oRequest = new Amazon.S3.Model.ListObjectsRequest();
                oRequest.WithBucketName(m_strBucketName).WithPrefix(m_oGetTreeNode.m_strPrefix);

                m_oGetTreeNode.m_oS3Client.BeginListObjects(oRequest, m_oGetTreeNode.m_fCallback, m_oGetTreeNode.m_oParam);
            }
            catch
            {
                m_oGetTreeNode.m_eError = ErrorTypes.StorageGetInfo;
                m_oGetTreeNode.DisposeAndCallback();
            }
        }
Exemplo n.º 10
0
        public StorageTreeNode GetTreeNode(string strPath)
        {
            StorageTreeNode oNode = null;
            try
            {
                using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion))
                {
                    string strPrefix = GetDirPath(strPath);
                    Amazon.S3.Model.ListObjectsRequest oRequest = new Amazon.S3.Model.ListObjectsRequest();
                    oRequest.WithBucketName(m_strBucketName).WithPrefix(strPrefix);

                    using (Amazon.S3.Model.ListObjectsResponse oResponse = oS3Client.ListObjects(oRequest))
                    {
                        oNode = new StorageTreeNode(strPrefix.Substring(0, strPrefix.Length - 1), true);
                        foreach (Amazon.S3.Model.S3Object entry in oResponse.S3Objects)
                        {
                            AddNodeRecursive(oNode, entry.Key.Substring(strPrefix.Length));
                        }
                    }
                }
            }
            catch
            {
            }

            return oNode;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Lists objects from the specified S3 bucket.
        /// </summary>
        /// <param name="bucket">The name of the bucket to get objects from.</param>
        /// <param name="prefix">The prefix used to filter any results. For example if you had
        /// a folder called img in the root of your bucket, the prefix used would be img/ to 
        /// get all keys in that folder with a delimiter of / to only return keys in that folder, not subfolders.</param>
        /// <param name="delimiter">Used to specify the end of what to return keys for. For example, to
        /// get all keys in folder a in the structure a/b/keys, you would use a/ as the prefix and
        /// / as the delimiter to only get keys inside a.</param>
        /// <param name="maxKeys">The maximum number of keys to return.</param>
        /// <param name="marker">The key to start with when returning keys. Keys are stored in alphabetical order in s3.</param>
        public static Amazon.S3.Model.ListObjectsResponse ListObjectsResponse(string bucket,
                                                                              string prefix = "",
                                                                              string delimiter = null,
                                                                              int maxKeys = 1000,
                                                                              string marker = null)
        {
            Amazon.S3.Model.ListObjectsResponse response = new Amazon.S3.Model.ListObjectsResponse();
            using (Amazon.S3.IAmazonS3 client = new Factory().S3Client())
            {
                Amazon.S3.Model.ListObjectsRequest request = new Amazon.S3.Model.ListObjectsRequest()
                {
                    BucketName = bucket,
                    Prefix = prefix,
                    MaxKeys = maxKeys
                };

                if (delimiter != null)
                    request.Delimiter = delimiter;

                if (marker != null)
                    request.Marker = marker;

                response = client.ListObjects(request);
            }
            return response;
        }
Exemplo n.º 12
0
        //
        // GET: /S3Object/
        public ActionResult Index(string prefix = "", int maxKeys = 100)
        {
            ViewBag.prefix = prefix;
              ViewBag.maxKeys = maxKeys;

              var list = new Amazon.S3.Model.ListObjectsRequest();
              list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              if (!String.IsNullOrWhiteSpace(prefix))
              {
            list.WithPrefix(prefix);
              }

              list.WithMaxKeys(maxKeys);

              var response = s3.ListObjects(list);

              return View(response.S3Objects.Select(e => new S3Object
            {
              Key = e.Key,
              Size = e.Size,
              LastModified = e.LastModified
            }));
        }
Exemplo n.º 13
0
        //
        // GET: /S3Object/Edit/5
        public ActionResult Edit(string id = null, string prefix = "", int maxKeys = 100)
        {
            ViewBag.prefix = prefix;
              ViewBag.maxKeys = maxKeys;

              var list = new Amazon.S3.Model.ListObjectsRequest();
              list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              list.WithPrefix(id);

              var s3Objects = s3.ListObjects(list).S3Objects;
              if (s3Objects.Count == 0)
              {
            return HttpNotFound();
              }

              var get = new Amazon.S3.Model.GetObjectRequest();
              get.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              get.WithKey(s3Objects[0].Key);

              var response = s3.GetObject(get);

              S3Object modelObject = new S3Object
              {
            Key = s3Objects[0].Key,
            Size = s3Objects[0].Size,
            LastModified = s3Objects[0].LastModified,
            ContentType = response.ContentType
              };

              return View(modelObject);
        }
Exemplo n.º 14
0
        public ActionResult BulkDeleteConfirmed(string prefix = "", int maxKeys = 100)
        {
            if (!String.IsNullOrWhiteSpace(prefix))
              {
            ViewBag.prefix = prefix;
            ViewBag.maxKeys = maxKeys;

            var list = new Amazon.S3.Model.ListObjectsRequest();
            list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
            if (!String.IsNullOrWhiteSpace(prefix))
            {
              list.WithPrefix(prefix);
            }

            list.WithMaxKeys(maxKeys);

            var keys = s3.ListObjects(list).S3Objects.Select(e => new Amazon.S3.Model.KeyVersion(e.Key)).ToArray();

            var bulkDelete = new Amazon.S3.Model.DeleteObjectsRequest();
            bulkDelete.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
            bulkDelete.WithKeys(keys);
            s3.DeleteObjects(bulkDelete);
              }

              return RedirectToAction("Index", new { prefix = prefix, maxKeys = maxKeys });
        }
Exemplo n.º 15
0
        //
        // GET: /S3Object/BulkDelete
        public ActionResult BulkDelete(string prefix = "", int maxKeys = 100)
        {
            ViewBag.prefix = prefix;
              ViewBag.maxKeys = maxKeys;

              var list = new Amazon.S3.Model.ListObjectsRequest();
              list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]);
              if (!String.IsNullOrWhiteSpace(prefix))
              {
            list.WithPrefix(prefix);
              }

              list.WithMaxKeys(maxKeys);

              var s3Objects = s3.ListObjects(list);
              List<S3Object> modelObjects = new List<S3Object>(s3Objects.S3Objects.Count);

              foreach (var s3Object in s3Objects.S3Objects)
              {
            modelObjects.Add(new S3Object
            {
              Key = s3Object.Key,
              Size = s3Object.Size,
              LastModified = s3Object.LastModified
            });
              }

              return View(modelObjects);
        }