// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX - License - Identifier: Apache - 2.0 using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Amazon; using Amazon.Runtime; using Amazon.S3; using Amazon.S3.Model; namespace S3Demo.Model { class ObjectModel { #region 上传对象(带标签) /// /// 带标签发送对象至存储桶 /// /// /// /// /// /// /// /// public static async Task Putobjectswithtags(string bucketName, string keyName, string filePath, string[] tags, BasicAWSCredentials credentials, AmazonS3Config conf) { //string bucketName = "doc-example-bucket"; //string keyName = "newobject.txt"; //string filePath = @"*** file path ***"; // Specify your bucket region (an example region is shown). //RegionEndpoint bucketRegion = RegionEndpoint.USWest2; var client = new AmazonS3Client(credentials, conf); string resv = await PutObjectsWithTagsAsync(client, bucketName, keyName, filePath, tags); return resv; } /// /// This method uploads an object with tags. It then shows the tag /// values, changes the tags, and shows the new tags. /// /// The Initialized Amazon S3 client object used /// to call the methods to create and change an objects tags. /// A string representing the name of the /// bucket where the object will be stored. /// A string representing the key name of the /// object to be tagged. /// The directory location and file name of the /// object to be uploaded to the S3 bucket. public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath, string[] tags) { try { // Create an object with tags. var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, TagSet = new List { new Tag { Key = "sn", Value = tags[0] }, new Tag { Key = "date", Value = tags[1] }, new Tag { Key = "code", Value = tags[2] } }, }; PutObjectResponse response = await client.PutObjectAsync(putRequest); return response.HttpStatusCode.ToString(); #region 查看tag //GetObjectTaggingRequest getTagsRequest = new() //{ // BucketName = bucketName, // Key = keyName, //}; //// 获取对象 //GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); //// Display the tag values. //objectTags.Tagging // .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); #endregion #region 替换新Tag(remove) //Tagging newTagSet = new() //{ // TagSet = new List // { // new Tag { Key = "Key3", Value = "Value3" }, // new Tag { Key = "Key4", Value = "Value4" }, // }, //}; //PutObjectTaggingRequest putObjTagsRequest = new() //{ // BucketName = bucketName, // Key = keyName, // Tagging = newTagSet, //}; //PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest); //// Retrieve the tags again and show the values. //GetObjectTaggingRequest getTagsRequest2 = new() //{ // BucketName = bucketName, // Key = keyName, //}; //GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2); //objectTags2.Tagging // .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); #endregion } catch (AmazonS3Exception ex) { Console.WriteLine( $"Error: '{ex.Message}'"); return ex.Message.ToString(); } } #endregion #region 查找对象 public static async Task Readobjectdata(string bucketName, string keyName, BasicAWSCredentials credentials, AmazonS3Config conf) { //const string bucketName = "doc-example-bucket"; //const string keyName = "filetodownload"; // If the Amazon Region where the S3 bucket was created is not // the same as the region defined for the default user, specify // the region as a parameter to the client constructor. // For example: RegionEndpoint.USWest2; IAmazonS3 client = new AmazonS3Client(credentials, conf); await ReadObjectDataAsync(client, bucketName, keyName); } /// /// This method copies the contents of the object keyName to another /// location, for example, to your local system. /// /// The initialize S3 client used to call /// GetObjectAsync. /// The name of the S3 bucket which contains /// the object to copy. /// The name of the object you want to copy. static async Task ReadObjectDataAsync(IAmazonS3 client, string bucketName, string keyName) { string responseBody = string.Empty; try { GetObjectRequest request = new GetObjectRequest { BucketName = bucketName, Key = keyName, }; // Get tag GetObjectTaggingRequest getTagsRequest = new() { BucketName = bucketName, Key = keyName, }; GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); // Display the tag values. objectTags.Tagging .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); using (GetObjectResponse response = await client.GetObjectAsync(request)) using (Stream responseStream = response.ResponseStream) using (StreamReader reader = new StreamReader(responseStream)) { // Assume you have "title" as medata added to the object. string title = response.Metadata["x-amz-meta-title"]; string contentType = response.Headers["Content-Type"]; Console.WriteLine($"Object keyName: {response.Key}"); Console.WriteLine($"Object metadata, Title: {title}"); Console.WriteLine($"Content type: {contentType}"); // Retrieve the contents of the file. responseBody = reader.ReadToEnd(); // Write the contents of the file to disk. string filePath = $"C:\\Temp\\copy_of_{keyName}"; } } catch (AmazonS3Exception e) { // If the bucket or the object do not exist Console.WriteLine($"Error: '{e.Message}'"); } } #endregion } }