Exemplo n.º 1
0
        private async Task TestConnection()
        {
            if (this.TestResult != null)
            {
                return;
            }

            m_lblTestResult.Visible = true;

            if (string.IsNullOrEmpty(this.AccessKey) || string.IsNullOrEmpty(this.SecretKey))
            {
                m_lblTestResult.Text = "Please enter Access Key and Secret Key";
                return;
            }

            if (m_chkUseSessionToken.Checked == true && string.IsNullOrEmpty(this.SessionToken))
            {
                m_lblTestResult.Text = "Please enter Session Token";
                return;
            }

            this.Enabled         = false;
            m_lblTestResult.Text = "Testing Connection...";

            this.TestResult = null;

            AWSCredentials credentials;

            if (m_chkUseSessionToken.Checked == false)
            {
                credentials = new BasicAWSCredentials(this.AccessKey, this.SecretKey);
            }
            else
            {
                credentials = new SessionAWSCredentials(this.AccessKey, this.SecretKey, this.SessionToken);
            }

            try
            {
                using (var api = AmazonS3Helper.GetApi(credentials, this.AWSRegion))
                {
                    var buckets = await api.ListBucketsAsync();

                    m_lblTestResult.Text = "Connection succeeded.";
                    this.TestResult      = true;
                }
            }
            catch (Exception ex)
            {
                m_lblTestResult.Text = "Connection failed!";
                this.TestResult      = false;
            }
            finally
            {
                this.Enabled = true;
            }
        }
Exemplo n.º 2
0
        public async Task Delete(string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                var response = await api.DeleteObjectAsync(bucket, filename);

                if (response == null)
                {
                    throw new InvalidOperationException("Delete for Amazon S3 failed.");
                }
            }
        }
Exemplo n.º 3
0
        public async Task Save(Stream stream, string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                try // Does parent folder exists, if not root?
                {
                    var folderName = CloudPath.GetDirectoryName(filename);

                    if (!string.IsNullOrEmpty(folderName))
                    {
                        await api.GetObjectMetadataAsync(bucket, folderName + "/");
                    }
                }
                catch (AmazonS3Exception ex)
                {
                    //if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                    //    throw new FileNotFoundException("Amazon S3: File not found.", );

                    //status wasn't not found, so throw the exception
                    throw;
                }

                var request = new PutObjectRequest
                {
                    BucketName  = bucket,
                    Key         = filename,
                    InputStream = stream
                };

                var response = await api.PutObjectAsync(request);

                if (response == null)
                {
                    throw new InvalidOperationException("Save to Amazon S3 failed.");
                }
            }
        }
        public async Task <Stream> Load(string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                var request = new GetObjectRequest
                {
                    BucketName = bucket,
                    Key        = filename
                };

                var response = await api.GetObjectAsync(request);

                return(response.ResponseStream);
            }
        }
Exemplo n.º 5
0
        public async Task Copy(string sourcePath, string destPath)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string sourceBucket;
                string sourceFilename;
                string destBucket;
                string destFilename;

                GetBucketAndKey(sourcePath, out sourceBucket, out sourceFilename);
                GetBucketAndKey(destPath, out destBucket, out destFilename);

                var response = await api.CopyObjectAsync(sourceBucket, sourceFilename, destBucket, destFilename);

                if (response == null)
                {
                    throw new InvalidOperationException("Copy for Amazon S3 failed.");
                }
            }
        }
        public async Task <bool> Save(Stream stream, string path)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                string bucket;
                string filename;

                GetBucketAndKey(path, out bucket, out filename);

                try // Does parent folder exists?
                {
                    var folderName  = CloudPath.GetDirectoryName(filename);
                    var getResponse = await api.GetObjectMetadataAsync(bucket, folderName + "/");
                }
                catch (AmazonS3Exception ex)
                {
                    if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        return(false);
                    }

                    //status wasn't not found, so throw the exception
                    throw;
                }

                var request = new PutObjectRequest
                {
                    BucketName  = bucket,
                    Key         = filename,
                    InputStream = stream
                };

                var response = await api.PutObjectAsync(request);

                return(response != null);
            }
        }
        public async Task <IEnumerable <StorageProviderItem> > GetChildrenByParentItem(StorageProviderItem parent)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                if (parent.Id == "/")
                {
                    var response = await api.ListBucketsAsync();

                    var items = response.Buckets.Select(_ => new StorageProviderItem
                    {
                        Id   = _.BucketName,
                        Name = _.BucketName,
                        Type = StorageProviderItemType.Folder,
                        ParentReferenceId = parent.Id
                    });

                    return(items.ToArray());
                }
                else
                {
                    string bucket;
                    string filename;
                    GetBucketAndKey(parent.Id, out bucket, out filename);
                    var filenameLength = string.IsNullOrEmpty(filename) ? 0 : filename.Length;

                    var request = new ListObjectsV2Request
                    {
                        BucketName = bucket,
                        Prefix     = filename,
                    };

                    var items = new List <StorageProviderItem>();
                    ListObjectsV2Response response;
                    var finished = false;

                    do
                    {
                        response = await api.ListObjectsV2Async(request);

                        foreach (var o in response.S3Objects)
                        {
                            var normalized = o.Key.Substring(filenameLength);
                            if (string.IsNullOrEmpty(normalized))
                            {
                                continue;
                            }

                            if (normalized.EndsWith("/"))
                            {
                                normalized = normalized.Remove(normalized.Length - 1);
                            }


                            if (normalized.Contains('/'))
                            {
                                finished = true;
                                break;
                            }

                            items.Add(new StorageProviderItem
                            {
                                Id   = bucket + "/" + o.Key,
                                Name = normalized,
                                Type = o.Size > 0 ? StorageProviderItemType.File : StorageProviderItemType.Folder,
                                ParentReferenceId = parent.Id,
                            });
                        }

                        if (finished)
                        {
                            break;
                        }

                        request.ContinuationToken = response.NextContinuationToken;
                    } while (response.IsTruncated);

                    return(items.ToArray());
                }
            }
        }
Exemplo n.º 8
0
        public async Task <IEnumerable <StorageProviderItem> > GetChildrenByParentItem(StorageProviderItem parent)
        {
            using (var api = AmazonS3Helper.GetApi(_account))
            {
                if (parent.Id == "/")
                {
                    var response = await api.ListBucketsAsync();

                    var items = response.Buckets.Select(_ => new StorageProviderItem
                    {
                        Id   = _.BucketName,
                        Name = _.BucketName,
                        Type = StorageProviderItemType.Folder,
                        ParentReferenceId = parent.Id
                    });

                    return(items.ToArray());
                }
                else
                {
                    string bucket;
                    string prefix;
                    GetBucketAndKey(parent.Id, out bucket, out prefix);

                    var request = new ListObjectsV2Request
                    {
                        BucketName = bucket,
                        Prefix     = prefix,
                        Delimiter  = "/",
                    };

                    var items = new List <StorageProviderItem>();
                    ListObjectsV2Response response;

                    do
                    {
                        response = await api.ListObjectsV2Async(request);

                        items.AddRange(response.CommonPrefixes.Select(folderName => new StorageProviderItem
                        {
                            Id   = bucket + "/" + folderName,
                            Name = folderName.RemovePrefix(prefix).RemoveTrailingSlash(),
                            Type = StorageProviderItemType.Folder,
                            ParentReferenceId = parent.Id,
                        }));

                        foreach (var o in response.S3Objects)
                        {
                            var normalized = o.Key.RemovePrefix(prefix);

                            if (string.IsNullOrEmpty(normalized) || normalized.EndsWith("/")) // Is Parent Folder (Dummy Item)? => ignore
                            {
                                continue;
                            }

                            items.Add(new StorageProviderItem
                            {
                                Id   = bucket + "/" + o.Key,
                                Name = normalized,
                                Type = StorageProviderItemType.File,
                                ParentReferenceId    = parent.Id,
                                LastModifiedDateTime = o.LastModified
                            });
                        }

                        request.ContinuationToken = response.NextContinuationToken;
                    } while (response.IsTruncated);

                    return(items.ToArray());
                }
            }
        }