|
|
|
|
// 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 上传对象(带标签)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 带标签发送对象至存储桶
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bucketName"></param>
|
|
|
|
|
/// <param name="keyName"></param>
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
|
|
/// <param name="tags"></param>
|
|
|
|
|
/// <param name="credentials"></param>
|
|
|
|
|
/// <param name="conf"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<string> 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
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] },
|
|
|
|
|
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<Tag>
|
|
|
|
|
// {
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
}
|
|
|
|
|
}
|