/// <summary> /// 查看搜狐云文件 /// </summary> /// <param name="key">查询文件名称</param> /// <returns></returns> public string QuerySoHuYunInfo(string key) { StringBuilder sb = new StringBuilder(); #region 验证密钥 string accessKey = System.Configuration.ConfigurationManager.AppSettings["accessKey"]; string secretKey = System.Configuration.ConfigurationManager.AppSettings["secretKey"]; SHSCSCredentials myCredentials = new BasicSHSCSCredentials(accessKey, secretKey); #endregion SohuCSClient client = new SohuCSClient(myCredentials, RegionEndpoint.BJCNC); GetObjectMetadataRequest request1 = new GetObjectMetadataRequest { BucketName = "shangpin", Key = key, VersionId = "" }; try { GetObjectMetadataResponse response1 = client.GetObjectMetadata(request1); Console.WriteLine("{0}", response1.LastModified); foreach (string sk in response1.Metadata.Keys) { sb.Append(string.Format("{0}-{1}", sk, response1.Metadata[sk])); } return(sb.ToString()); } catch (Exception ex) { throw ex; } }
// Always returns an object even if file doesn't exist public FileMetadata FileMetadata(string name) { FileMetadata results = new FileMetadata { Name = name }; using (var client = new AmazonS3Client(this.amazonKey, this.amazonSecret, this.amazonRegion)) { GetObjectMetadataRequest request = new GetObjectMetadataRequest { BucketName = this.amazonBucket, Key = name }; try { GetObjectMetadataResponse response = client.GetObjectMetadata(request); results.Exists = true; // else AWSSDK threw results.Length = response.ContentLength; results.LastModified = response.LastModified; } catch (AmazonS3Exception ex) { if (ex.ErrorCode == "NoSuchKey" || ex.ErrorCode == "NotFound") { results.Exists = false; // File doesn't exist } else { throw; } } } return(results); }
public static DateTime?S3FileExists(string filename, IConfiguration configuration) { try { Init(configuration); IAmazonS3 client = new AmazonS3Client(s3_access_key_id, s3_secret_access_key, Amazon.RegionEndpoint.USEast1); var request = new GetObjectMetadataRequest { BucketName = s3_access_point, Key = filename.Replace('\\', '/') }; GetObjectMetadataResponse response = client.GetObjectMetadataAsync(request).Result; Console.WriteLine("File: " + filename + " exists in S3 - last modified: " + response.LastModified); return(response.LastModified); } catch (Exception ex) { Console.WriteLine("File: " + filename + " not found in S3"); // Console.WriteLine(ex.ToString()); return(null); } }
public static bool file_exists(string fileName) { try { AmazonS3Client client = get_client(); if (client == null) { return(false); } GetObjectMetadataRequest request = new GetObjectMetadataRequest(); request.BucketName = RaaiVanSettings.CephStorage.Bucket; request.Key = fileName; GetObjectMetadataResponse response = client.GetObjectMetadata(request); return(true); } catch (AmazonS3Exception ex) { if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return(false); } return(false); } }
public long CheckFileSize(string key) { long size = -1; try { GetObjectMetadataRequest req = new GetObjectMetadataRequest(); req.BucketName = this._bucketName; req.Key = key.Replace(bucketUrl, ""); GetObjectMetadataResponse res = _awsS3Client.GetObjectMetadata(req); if (res.HttpStatusCode == System.Net.HttpStatusCode.OK) { size = res.ContentLength; } } catch (AmazonS3Exception s3Ex) { if (s3Ex.ErrorCode != "NotFound") { throw s3Ex; } } catch (Exception ex) { throw ex; } return(size); }
public static bool FileExist(string FileName, string Bucketname) { bool fileFound = true; var s3 = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.EUWest1); GetObjectMetadataResponse response = new GetObjectMetadataResponse(); try { response = s3.GetObjectMetadata(new GetObjectMetadataRequest() .WithBucketName(Bucketname) .WithKey(FileName)); fileFound = true; } catch (Amazon.S3.AmazonS3Exception ex) { if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { fileFound = false; } //status wasn't not found, so throw the exception //throw; } return(fileFound); }
public async Task CopyObjectTest() { // ARRANGE CopyObjectRequest request = new CopyObjectRequest() { DestinationBucket = "mhaken", SourceBucket = "mhaken-lambda", SourceKey = "AWSAthenaUserMetrics/athena-metrics-636765132762278062.zip", DestinationKey = "test/file.txt", }; GetObjectMetadataRequest meta = new GetObjectMetadataRequest() { BucketName = request.SourceBucket, Key = request.SourceKey }; GetObjectMetadataResponse Meta = await client.GetObjectMetadataAsync(meta); // ACT CopyObjectResponse Response = await client.CopyOrMoveObjectAsync(request, 16777216, false); // ASSERT Assert.Equal(HttpStatusCode.OK, Response.HttpStatusCode); Assert.Equal(Meta.ETag, Response.ETag); }
public bool CheckObjectExists(string key) { bool exists = false; try { GetObjectMetadataRequest req = new GetObjectMetadataRequest(); req.BucketName = this._bucketName; req.Key = key.Replace(bucketUrl, ""); GetObjectMetadataResponse res = _awsS3Client.GetObjectMetadata(req); if (res.HttpStatusCode == System.Net.HttpStatusCode.OK) { exists = true; } } catch (AmazonS3Exception s3Ex) { if (s3Ex.ErrorCode != "NotFound") { throw s3Ex; } else { exists = false; } } catch (Exception ex) { throw ex; } return(exists); }
public async Task MoveObjectTest() { // ARRANGE CopyObjectRequest req = new CopyObjectRequest() { DestinationBucket = destinationBucket, SourceBucket = sourceBucket, SourceKey = "test/file.txt", DestinationKey = "test/file2.txt", }; GetObjectMetadataRequest metaReq = new GetObjectMetadataRequest() { BucketName = req.SourceBucket, Key = req.SourceKey }; GetObjectMetadataResponse meta = await client.GetObjectMetadataAsync(metaReq); // ACT CopyObjectResponse response = await client.CopyOrMoveObjectAsync(req, 16777216, true); // ASSERT Assert.Equal(HttpStatusCode.OK, response.HttpStatusCode); Assert.Equal(meta.ETag, response.ETag); }
public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) { GetObjectMetadataResponse getObjectMetadataResponse = new GetObjectMetadataResponse(); UnmarshallResult(context, getObjectMetadataResponse); return(getObjectMetadataResponse); }
public override void DeleteExpired(string domain, string path, TimeSpan oldThreshold) { using (AmazonS3 client = GetClient()) { IEnumerable <S3Object> s3Obj = GetS3Objects(domain, path); foreach (S3Object s3Object in s3Obj) { GetObjectMetadataRequest request = new GetObjectMetadataRequest().WithBucketName(_bucket).WithKey(s3Object.Key); using (GetObjectMetadataResponse metadata = client.GetObjectMetadata(request)) { string privateExpireKey = metadata.Metadata["private-expire"]; if (!string.IsNullOrEmpty(privateExpireKey)) { long fileTime; if (long.TryParse(privateExpireKey, out fileTime)) { if (DateTime.UtcNow > DateTime.FromFileTimeUtc(fileTime)) { //Delete it DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest().WithBucketName(_bucket).WithKey(s3Object.Key); using (client.DeleteObject(deleteObjectRequest)) { } } } } } } } }
private bool ExistsKey(AmazonS3Client client, string key, string bucketName) { GetObjectMetadataRequest request = new GetObjectMetadataRequest(); request.BucketName = bucketName; request.Key = key; bool ok = false; try { GetObjectMetadataResponse response = client.GetObjectMetadata(request); if (response != null) { ok = (response.HttpStatusCode == HttpStatusCode.OK); } } catch (AmazonS3Exception e) { if (e.Message == "The specified key does not exist") { ok = false; } else { throw; } } return(ok); }
public S3FileSystemItem(string name, GetObjectMetadataResponse file) { this.Name = name; this.Size = file.ContentLength; this.IsDirectory = false; this.LastModifyTime = new DateTimeOffset(file.LastModified, TimeSpan.Zero); }
public override async Task <IBlobMetadata> FetchMetadataAsync(string virtualPath, NameValueCollection queryString) { var path = ParseAndFilterPath(virtualPath); //Looks like we have to execute a head request var request = new GetObjectMetadataRequest() { BucketName = path.Bucket, Key = path.Key }; try { GetObjectMetadataResponse response = await S3Client.GetObjectMetadataAsync(request); return(new BlobMetadata() { Exists = true, LastModifiedDateUtc = response.LastModified }); } catch (AmazonS3Exception s3e) { if (s3e.StatusCode == System.Net.HttpStatusCode.NotFound || s3e.StatusCode == System.Net.HttpStatusCode.Forbidden) { return(new BlobMetadata() { Exists = false }); } else { throw; } } }
public bool UploadTest() { TransferUtilityUploadRequest request = new TransferUtilityUploadRequest(); request.BucketName = "eecs393minesweeper"; String filename = "testmap"; request.FilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Minesweeper\\" + filename + ".map"; utility.Upload(request); ReadOnlineFiles(); PopulateOnlineList(); GetObjectMetadataRequest req = new GetObjectMetadataRequest(); req.BucketName = "eecs393minesweeper"; String key = "test.map"; req.Key = key; try { GetObjectMetadataResponse response = client.GetObjectMetadata(req.BucketName, req.Key); } catch (AmazonS3Exception e) { if (e.Message == "The specified key does not exist") { return(false); } throw; } return(true); }
public long FileLength(string filename, long lenghtFallback) { try { S3FileInfo s3FileInfo = new S3FileInfo(_s3Client, _containerName, _rootFolderName + filename); GetObjectMetadataRequest request = new GetObjectMetadataRequest(); request.Key = _rootFolderName + filename; request.BucketName = _containerName; GetObjectMetadataResponse response = _s3Client.GetObjectMetadata(request); var CachedLength = response.Metadata["CachedLength"]; if (!string.IsNullOrEmpty(CachedLength) && long.TryParse(CachedLength, out var ObjectLength)) { return(ObjectLength); } return(s3FileInfo.Length); } catch (Exception e) { // Sync(name); Trace.WriteLine( $"ERROR {e.ToString()} Exception thrown while retrieving file length of file {filename} for {_rootFolderName}"); return(lenghtFallback); } }
private async Task <Tuple <string, MetadataCollection> > GetMetadata(string secretVersion, string objectKey, AmazonS3Client s3Client) { var getObjMetadataReq = new GetObjectMetadataRequest { BucketName = BucketName, Key = objectKey }; if (!string.IsNullOrEmpty(secretVersion)) { getObjMetadataReq.VersionId = secretVersion; } GetObjectMetadataResponse metadataResponse = null; try { metadataResponse = await s3Client.GetObjectMetadataAsync(getObjMetadataReq); } catch (AmazonS3Exception ex) { if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return(null); } throw ex; } return(new Tuple <string, MetadataCollection>(metadataResponse.VersionId, metadataResponse.Metadata)); }
static async Task WritingAnObjectAsync() { try { var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, ContentBody = "sample text", ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }; var putResponse = await client.PutObjectAsync(putRequest); // Determine the encryption state of an object. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = keyName }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); ServerSideEncryptionMethod objectEncryption = response.ServerSideEncryptionMethod; Console.WriteLine("Encryption method used: {0}", objectEncryption.ToString()); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } }
/// <summary> /// Upload a sample object include a setting for encryption. /// </summary> /// <param name="client">The initialized S3 client object used to /// to upload a file and apply server-side encryption.</param> /// <param name="bucketName">The name of the S3 bucket where the /// encrypted object will reside.</param> /// <param name="keyName">The name for the object that you want to /// create in the supplied bucket.</param> public static async Task WritingAnObjectAsync(IAmazonS3 client, string bucketName, string keyName) { try { var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, ContentBody = "sample text", ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256, }; var putResponse = await client.PutObjectAsync(putRequest); // Determine the encryption state of an object. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = keyName, }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); ServerSideEncryptionMethod objectEncryption = response.ServerSideEncryptionMethod; Console.WriteLine($"Encryption method used: {0}", objectEncryption.ToString()); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: '{ex.Message}' when writing an object"); } }
/// <summary> /// 获取对象信息 /// </summary> /// <param name="bucketName"></param> /// <param name="objectName"></param> /// <param name="versionID"></param> /// <param name="matchEtag"></param> /// <param name="modifiedSince"></param> /// <returns></returns> public Task <ItemMeta> GetObjectMetadataAsync(string bucketName, string objectName, string versionID = null, string matchEtag = null, DateTime?modifiedSince = null) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); GetObjectMetadataRequest request = new GetObjectMetadataRequest() { BucketName = bucketName, ObjectKey = objectName, VersionId = versionID, }; GetObjectMetadataResponse response = _client.GetObjectMetadata(request); var newMeta = new ItemMeta() { ObjectName = objectName, ContentType = response.ContentType, Size = response.ContentLength, LastModified = response.LastModified.GetValueOrDefault(), ETag = response.ETag, IsEnableHttps = Options.IsEnableHttps, MetaData = new Dictionary <string, string>(), }; if (response.Metadata != null && response.Metadata.Count > 0) { foreach (var item in response.Metadata.KeyValuePairs) { newMeta.MetaData.Add(item.Key, item.Value); } } return(Task.FromResult(newMeta)); }
public static void Main(string[] args) { // create the ECS S3 Client ECSS3Client s3 = ECSS3Factory.getS3Client(); // retrieve the object key from the user Console.Write("Enter the object key: "); string key = Console.ReadLine(); // create object metadata request GetObjectMetadataRequest request = new GetObjectMetadataRequest() { BucketName = ECSS3Factory.S3_BUCKET, Key = key }; // get object metadata - not actual content (HEAD request not GET). GetObjectMetadataResponse response = s3.GetObjectMetadata(request); // print out object key/value and metadata key/value for validation Console.WriteLine(string.Format("Metadata for {0}/{1}", ECSS3Factory.S3_BUCKET, key)); MetadataCollection metadataCollection = response.Metadata; ICollection <string> metaKeys = metadataCollection.Keys; foreach (string metaKey in metaKeys) { Console.WriteLine("{0}={1}", metaKey, metadataCollection[metaKey]); } Console.ReadLine(); }
static void Main(string[] args) { IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); // List to store upload part responses. List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); List<CopyPartResponse> copyResponses = new List<CopyPartResponse>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey }; InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest); String uploadId = initResponse.UploadId; try { // Get object size. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = sourceBucket, Key = sourceObjectKey }; GetObjectMetadataResponse metadataResponse = s3Client.GetObjectMetadata(metadataRequest); long objectSize = metadataResponse.ContentLength; // in bytes // Copy parts. long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { CopyPartRequest copyRequest = new CopyPartRequest { DestinationBucket = targetBucket, DestinationKey = targetObjectKey, SourceBucket = sourceBucket, SourceKey = sourceObjectKey, UploadId = uploadId, FirstByte = bytePosition, LastByte = bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1, PartNumber = i }; copyResponses.Add(s3Client.CopyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = targetBucket, Key = targetObjectKey, UploadId = initResponse.UploadId }; completeRequest.AddPartETags(copyResponses); CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); } catch (Exception e) { Console.WriteLine(e.Message); } }
public RandomAccessS3Stream(S3FileSystem fileSystem, GetObjectMetadataResponse objectMetadata, string bucketName, string key) { this.fileSystem = fileSystem; this.objectMetadata = objectMetadata; this.bucketName = bucketName; this.key = key; }
/// <summary> /// Initializes a new instance of the <see cref="AwsS3ReadonlyStream" /> class. /// </summary> /// <param name="s3">The <see cref="IAmazonS3"/>.</param> /// <param name="s3Uri">The <see cref="AmazonS3Uri"/>.</param> /// <param name="s3ObjectMetadata">The <see cref="GetObjectMetadataResponse"/>.</param> public AwsS3ReadonlyStream(IAmazonS3 s3, AmazonS3Uri s3Uri, GetObjectMetadataResponse s3ObjectMetadata) { this.s3 = s3 ?? throw new ArgumentNullException(nameof(s3)); this.s3Uri = s3Uri ?? throw new ArgumentNullException(nameof(s3Uri)); Length = s3ObjectMetadata.ContentLength; s3ObjectEtag = s3ObjectMetadata.ETag; }
public async Task ShouldNotUploadNewVersionOfDirectoryArtifactWhenHashesMatch() { using var templateDir = this.deepNestedStack; // Hash of lambda directory content before zipping // Zips are not idempotent - fields e.g. timestamps in central directory change with successive zips of the same content. var directoryHash = new DirectoryInfo(Path.Combine(templateDir, "lambdacomplex")).MD5(); var template = Path.Combine(templateDir, "base-stack.json"); var projectId = S3Util.GenerateProjectId(template); var logger = new TestLogger(this.output); var mockSts = TestHelpers.GetSTSMock(); var mockS3 = TestHelpers.GetS3ClientWithBucketMock(); var mockContext = new Mock <IPSCloudFormationContext>(); mockContext.Setup(c => c.Logger).Returns(logger); mockContext.Setup(c => c.Region).Returns(RegionEndpoint.EUWest1); mockS3.SetupSequence(s3 => s3.ListObjectsV2Async(It.IsAny <ListObjectsV2Request>(), default)).ReturnsAsync( new ListObjectsV2Response { S3Objects = new List <S3Object> { new S3Object { BucketName = "test-bucket", Key = $"lambdacomplex-{projectId}-0000.zip" } } }).ReturnsAsync(this.fileNotFound).ReturnsAsync(this.fileNotFound); mockS3.Setup(s3 => s3.GetObjectMetadataAsync(It.IsAny <GetObjectMetadataRequest>(), default)).ReturnsAsync( () => { var resp = new GetObjectMetadataResponse(); resp.Metadata.Add(S3Util.PackagerHashKey, directoryHash); return(resp); }); var mockClientFactory = new Mock <IPSAwsClientFactory>(); mockClientFactory.Setup(f => f.CreateS3Client()).Returns(mockS3.Object); mockClientFactory.Setup(f => f.CreateSTSClient()).Returns(mockSts.Object); using var workingDirectory = new TempDirectory(); var packager = new PackagerUtils( new TestPathResolver(), logger, new S3Util(mockClientFactory.Object, mockContext.Object, template, "test-bucket", null, null), new OSInfo()); var outputTemplatePath = await packager.ProcessTemplate(template, workingDirectory); this.output.WriteLine(string.Empty); this.output.WriteLine(await File.ReadAllTextAsync(outputTemplatePath)); // Three objects should have been uploaded to S3 mockS3.Verify(m => m.PutObjectAsync(It.IsAny <PutObjectRequest>(), default), Times.Exactly(2)); }
public void ServerSideEncryptionBYOKTransferUtility() { var bucketName = S3TestUtils.CreateBucket(Client); try { Aes aesEncryption = Aes.Create(); aesEncryption.KeySize = 256; aesEncryption.GenerateKey(); string base64Key = Convert.ToBase64String(aesEncryption.Key); TransferUtility utility = new TransferUtility(Client); var uploadRequest = new TransferUtilityUploadRequest { BucketName = bucketName, Key = key, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key }; uploadRequest.InputStream = new MemoryStream(UTF8Encoding.UTF8.GetBytes("Encrypted Content")); utility.Upload(uploadRequest); GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = key, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key }; GetObjectMetadataResponse getObjectMetadataResponse = Client.GetObjectMetadata(getObjectMetadataRequest); Assert.AreEqual(ServerSideEncryptionCustomerMethod.AES256, getObjectMetadataResponse.ServerSideEncryptionCustomerMethod); var openRequest = new TransferUtilityOpenStreamRequest { BucketName = bucketName, Key = key, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key }; using (var stream = new StreamReader(utility.OpenStream(openRequest))) { var content = stream.ReadToEnd(); Assert.AreEqual(content, "Encrypted Content"); } } finally { AmazonS3Util.DeleteS3BucketWithObjects(Client, bucketName); } }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if (version != null) { getACLRequest.VersionId = version; } GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); GetObjectMetadataResponse getMetadataResponse = s3Client.GetObjectMetadata(new GetObjectMetadataRequest() .WithBucketName(bucketName) .WithKey(key)); // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.ServerSideEncryptionMethod = getMetadataResponse.ServerSideEncryptionMethod; if (version != null) { copyRequest.SourceVersionId = version; } copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) { setACLRequest.VersionId = copyResponse.VersionId; } setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
/// <summary> /// 结束对获取对象属性的异步请求。 /// </summary> /// <param name="ar">异步请求的响应结果。</param> /// <returns>获取对象属性的响应结果。</returns> public GetObjectMetadataResponse EndGetObjectMetadata(IAsyncResult ar) { GetObjectMetadataResponse response = this.EndDoRequest <GetObjectMetadataRequest, GetObjectMetadataResponse>(ar); HttpObsAsyncResult result = ar as HttpObsAsyncResult; GetObjectMetadataRequest request = result.AdditionalState as GetObjectMetadataRequest; response.BucketName = request.BucketName; response.ObjectKey = request.ObjectKey; return(response); }
public ObjectInfo(string objectName, GetObjectMetadataResponse stat) { ObjectName = objectName; Size = stat.ContentLength; LastModified = stat.LastModified; ETag = stat.ETag; MetaData = stat.ResponseMetadata.Metadata; Expires = stat.Expires == DateTime.MinValue ? DateTime.MaxValue : stat.Expires; ContentType = stat.Headers.ContentType; }
/// <summary> /// Sets up the request needed to make an exact copy of the object leaving the parent method /// the ability to change just the attribute being requested to change. /// </summary> /// <param name="bucketName"></param> /// <param name="key"></param> /// <param name="version"></param> /// <param name="s3Client"></param> /// <param name="copyRequest"></param> /// <param name="putACLRequest"></param> static void SetupForObjectModification(IAmazonS3 s3Client, string bucketName, string key, string version, out CopyObjectRequest copyRequest, out PutACLRequest putACLRequest) { // Get the existing ACL of the object GetACLRequest getACLRequest = new GetACLRequest(); getACLRequest.BucketName = bucketName; getACLRequest.Key = key; if (version != null) { getACLRequest.VersionId = version; } GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. putACLRequest = new PutACLRequest(); putACLRequest.BucketName = bucketName; putACLRequest.Key = key; putACLRequest.AccessControlList = getACLResponse.AccessControlList; ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest { BucketName = bucketName, Prefix = key, MaxKeys = 1 }); if (listObjectResponse.S3Objects.Count != 1) { throw new InvalidOperationException("No object exists with this bucket name and key."); } GetObjectMetadataRequest getMetaRequest = new GetObjectMetadataRequest() { BucketName = bucketName, Key = key }; GetObjectMetadataResponse getMetaResponse = s3Client.GetObjectMetadata(getMetaRequest); // Set the storage class on the object copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy; if (version != null) { copyRequest.SourceVersionId = version; } copyRequest.WebsiteRedirectLocation = getMetaResponse.WebsiteRedirectLocation; copyRequest.ServerSideEncryptionMethod = getMetaResponse.ServerSideEncryptionMethod; }