예제 #1
0
 public void UploadDirS3Tranfer(string AccessKey, string SerectKey, string bucketName, string endPoint, string DirectoryPath, EventHandler <UploadDirectoryProgressArgs> UploadProgressEvent = null)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var uploadRequest = new TransferUtilityUploadDirectoryRequest
             {
                 Directory     = DirectoryPath,
                 BucketName    = bucketName,
                 SearchPattern = "*.zip",
                 SearchOption  = System.IO.SearchOption.AllDirectories,
                 KeyPrefix     = Guid.NewGuid().ToString(),
                 CannedACL     = S3CannedACL.PublicRead
             };
             uploadRequest.UploadDirectoryProgressEvent += UploadProgressEvent;
             using (var Utility = new TransferUtility(client))
             {
                 Utility.UploadDirectory(uploadRequest);
             }
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #2
0
 public void UploadFileS3Tranfer(string AccessKey, string SerectKey, string bucketName, string keyName, string endPoint, string Path, EventHandler <UploadProgressArgs> UploadProgressEvent)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var uploadRequest = new TransferUtilityUploadRequest
             {
                 BucketName = bucketName,
                 Key        = keyName,
                 PartSize   = 6291456, // 6 MB
                 CannedACL  = S3CannedACL.PublicRead,
                 FilePath   = Path
             };
             uploadRequest.UploadProgressEvent += UploadProgressEvent;
             using (var Utility = new TransferUtility(client))
             {
                 Utility.Upload(uploadRequest);
             }
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #3
0
 /// <summary>
 /// Get list GetListS3BucketObject
 /// </summary>
 /// <param name="AccessKey"></param>
 /// <param name="SerectKey"></param>
 /// <param name="bucketName"></param>
 /// <param name="keyName"></param>
 /// <param name="endPoint"></param>
 /// <returns></returns>
 public List <ModelS3BucketObject> GetListS3BucketObject(string AccessKey, string SerectKey, string bucketName, string endPoint)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var listRequest = new ListObjectsRequest
             {
                 BucketName = bucketName,
             };
             ListObjectsResponse listResponse;
             do
             {
                 listResponse = client.ListObjects(listRequest);
                 var list = listResponse.S3Objects.Select(x => new ModelS3BucketObject
                 {
                     Key          = x.Key,
                     Size         = x.Size,
                     BucketName   = x.BucketName,
                     ETag         = x.ETag.Replace(@"""", string.Empty),
                     LastModified = x.LastModified,
                 }).ToList();
                 return(list);
             }while (listResponse.IsTruncated);
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #4
0
 /// <summary>
 /// Get List S3Bucket
 /// </summary>
 /// <param name="AccessKey"></param>
 /// <param name="SerectKey"></param>
 /// <param name="bucketName"></param>
 /// <param name="keyName"></param>
 /// <param name="endPoint"></param>
 /// <returns></returns>
 public List <ModelS3Bucket> GetListS3Bucket(string AccessKey, string SerectKey, string endPoint)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var response = client.ListBuckets();
             var List     = response.Buckets.Select(x => new ModelS3Bucket
             {
                 BucketName   = x.BucketName,
                 CreationDate = x.CreationDate
             }).ToList();
             return(List);
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #5
0
 /// <summary>
 /// Get Link Storage
 /// </summary>
 /// <param name="AccessKey"></param>
 /// <param name="SerectKey"></param>
 /// <param name="bucketName"></param>
 /// <param name="keyName"></param>
 /// <param name="endPoint"></param>
 /// <returns></returns>
 public string GetLinkS3(string AccessKey, string SerectKey, string bucketName, string keyName, string endPoint)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
             {
                 BucketName = bucketName,
                 Key        = keyName,
                 Expires    = DateTime.Now.AddHours(1),
                 Protocol   = Protocol.HTTPS
             };
             return(client.GetPreSignedURL(request));
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #6
0
 /// <summary>
 /// Get GetSingleObject
 /// </summary>
 /// <param name="AccessKey"></param>
 /// <param name="SerectKey"></param>
 /// <param name="bucketName"></param>
 /// <param name="keyName"></param>
 /// <param name="endPoint"></param>
 /// <returns></returns>
 public ModelS3BucketObject GetSingleObject(string AccessKey, string SerectKey, string bucketName, string keyName, string endPoint)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var request = new GetPreSignedUrlRequest
             {
                 BucketName = bucketName,
                 Key        = keyName,
                 Expires    = DateTime.Now.AddHours(1),
                 Protocol   = Protocol.HTTPS
             };
             //return client.GetPreSignedURL(request);
             var listRequest = new ListObjectsRequest
             {
                 BucketName = bucketName,
             };
             ListObjectsResponse listResponse;
             do
             {
                 listResponse = client.ListObjects(listRequest);
                 var Result = listResponse.S3Objects.Select(x => new ModelS3BucketObject
                 {
                     Key          = x.Key,
                     Size         = x.Size,
                     BucketName   = x.BucketName,
                     ETag         = x.ETag.Replace(@"""", string.Empty),
                     Link         = client.GetPreSignedURL(request),
                     LastModified = x.LastModified
                 }).FirstOrDefault();
                 return(Result);
             }while (listResponse.IsTruncated);
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }
예제 #7
0
 public void DownloadFileS3Tranfer(string AccessKey, string SerectKey, string bucketName, string keyName, string endPoint, string DestPath, EventHandler <WriteObjectProgressArgs> WriteObjectProgressEvent)
 {
     try
     {
         using (var client = new AmazonS3Client(AccessKey, SerectKey, RegionEndpointS3.ParseRegion(endPoint)))
         {
             var downloadRequest = new TransferUtilityDownloadRequest
             {
                 Key        = keyName,
                 BucketName = bucketName,
                 FilePath   = DestPath,
             };
             downloadRequest.WriteObjectProgressEvent += WriteObjectProgressEvent;
             using (var Utility = new TransferUtility(client))
             {
                 Utility.Download(downloadRequest);
             }
         }
     }
     catch (AmazonS3Exception ex)
     {
         throw new AmazonS3Exception(string.Format("Message {0} Error Code {1} Status Code {2}", ex.Message, ex.ErrorCode, ex.StatusCode));
     }
 }