You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
7.3 KiB

3 years ago
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 years ago
// SPDX - License - Identifier: Apache - 2.0
3 years ago
3 years ago
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;
3 years ago
namespace S3Demo.Helper
3 years ago
{
3 years ago
class ObjectHelper
3 years ago
{
#region 上传对象(带标签)
3 years ago
/// <summary>
/// This method uploads an object with tags. It then shows the tag
/// values, changes the tags, and shows the new tags.
/// </summary>
/// <param name="client">The Initialized Amazon S3 client object used
/// to call the methods to create and change an objects tags.</param>
/// <param name="bucketName">A string representing the name of the
/// bucket where the object will be stored.</param>
/// <param name="keyName">A string representing the key name of the
/// object to be tagged.</param>
/// <param name="filePath">The directory location and file name of the
/// object to be uploaded to the S3 bucket.</param>
public static async Task<string> PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath, string[] tags)
3 years ago
{
try
{
// Create an object with tags.
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath,
TagSet = new List<Tag>
{
new Tag { Key = "sn", Value = tags[0] },
new Tag { Key = "date", Value = tags[1] }
//TODO: 后续添加不良代码,接口获取
//new Tag { Key = "code", Value = tags[2] }
3 years ago
},
};
PutObjectResponse response = await client.PutObjectAsync(putRequest);
3 years ago
//Console.WriteLine(putRequest.Key + "," + putRequest.BucketName + "," + putRequest.FilePath + "," + response.HttpStatusCode);
return response.HttpStatusCode.ToString();
3 years ago
#region 查看tag
//GetObjectTaggingRequest getTagsRequest = new()
//{
// BucketName = bucketName,
// Key = keyName,
//};
//// 获取对象
//GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest);
3 years ago
//// Display the tag values.
//objectTags.Tagging
// .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
#endregion
3 years ago
#region 替换新Tag(remove)
//Tagging newTagSet = new()
//{
// TagSet = new List<Tag>
// {
// new Tag { Key = "Key3", Value = "Value3" },
// new Tag { Key = "Key4", Value = "Value4" },
// },
//};
3 years ago
//PutObjectTaggingRequest putObjTagsRequest = new()
//{
// BucketName = bucketName,
// Key = keyName,
// Tagging = newTagSet,
//};
3 years ago
//PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest);
3 years ago
//// Retrieve the tags again and show the values.
//GetObjectTaggingRequest getTagsRequest2 = new()
//{
// BucketName = bucketName,
// Key = keyName,
//};
//GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2);
3 years ago
//objectTags2.Tagging
// .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
#endregion
3 years ago
}
catch (Exception ex)
3 years ago
{
Console.WriteLine($"Error: {ex.Message}");
return ex.Message.ToString();
3 years ago
}
}
3 years ago
#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);
}
/// <summary>
/// This method copies the contents of the object keyName to another
/// location, for example, to your local system.
/// </summary>
/// <param name="client">The initialize S3 client used to call
/// GetObjectAsync.</param>
/// <param name="bucketName">The name of the S3 bucket which contains
/// the object to copy.</param>
/// <param name="keyName">The name of the object you want to copy.</param>
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
3 years ago
}
3 years ago
}