public void AWSListObjects(IAMUserSDKModel iamUserSDKDemoModel)
        {
            // Clean up view model data for display
            iamUserSDKDemoModel.AWSObjectList.Clear();
            iamUserSDKDemoModel.AWSErrorMsg = "";

            #region Get object in the bucket specified

            try
            {
                // Create AWS S3 client object - credential is 1) access key, 2) secret access key
                AmazonS3Client s3Client = new AmazonS3Client(MyAccessKeyID, MySecretAccessKey, Amazon.RegionEndpoint.USEast1);

                // Get list of objects in the specified bucket
                Amazon.S3.Model.ListObjectsResponse objectResponse = s3Client.ListObjects(iamUserSDKDemoModel.AWSBucketToGet);

                // Loop through the list of objects and set the view model to display
                foreach (Amazon.S3.Model.S3Object s3Object in objectResponse.S3Objects)
                {
                    iamUserSDKDemoModel.AWSObjectList.Add(s3Object.Key);
                }
            }
            catch (Exception ex)
            {
                // If effor occurs such as access denied from AWS, set the view model to display.
                iamUserSDKDemoModel.AWSErrorMsg = ex.Message;
            }


            #endregion

            return;
        }
        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);
        }
        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);
        }
        public StorageTreeNode GetTreeNodeEnd(IAsyncResult ar)
        {
            StorageTreeNode oNode = null;

            try
            {
                string strPrefix = m_oGetTreeNode.m_strPrefix;
                oNode = new StorageTreeNode(strPrefix.Substring(0, strPrefix.Length - 1), true);
                using (Amazon.S3.Model.ListObjectsResponse oResponse = m_oGetTreeNode.m_oS3Client.EndListObjects(ar))
                {
                    foreach (Amazon.S3.Model.S3Object oEntry in oResponse.S3Objects)
                    {
                        AddNodeRecursive(oNode, oEntry.Key.Substring(strPrefix.Length));
                    }
                }
            }
            catch
            {
            }
            return(oNode);
        }
            public override void Close()
            {
                try
                {
                    if (null != m_oStreamOutput)
                    {
                        m_oStreamOutput = null;
                    }
                    if (null != m_oS3Client)
                    {
                        m_oS3Client.Dispose();
                        m_oS3Client = null;
                    }

                    if (null != m_oGetObjectResponse)
                    {
                        m_oGetObjectResponse.Dispose();
                        m_oGetObjectResponse = null;
                    }

                    if (null != m_oListObjectsResponse)
                    {
                        m_oListObjectsResponse.Dispose();
                        m_oListObjectsResponse = null;
                    }

                    if (null != m_oDeleteObjectResponse)
                    {
                        m_oDeleteObjectResponse.Dispose();
                        m_oDeleteObjectResponse = null;
                    }
                }
                catch
                {
                }
            }
            public override void Close()
            {
                try
                {
                    if (null != m_oStreamOutput)
                    {

                       m_oStreamOutput = null;
                    }
                    if (null != m_oS3Client)
                    {
                        m_oS3Client.Dispose();
                        m_oS3Client = null;
                    }

                    if (null != m_oGetObjectResponse)
                    {
                        m_oGetObjectResponse.Dispose();
                        m_oGetObjectResponse = null;
                    }

                    if (null != m_oListObjectsResponse)
                    {
                        m_oListObjectsResponse.Dispose();
                        m_oListObjectsResponse = null;
                    }

                    if (null != m_oDeleteObjectResponse)
                    {
                        m_oDeleteObjectResponse.Dispose();
                        m_oDeleteObjectResponse = null;
                    }
                }
                catch
                {
                }
            }
Exemple #7
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;
        }