|
|
|
@ -1,23 +1,20 @@
|
|
|
|
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
// SPDX - License - Identifier: Apache - 2.0
|
|
|
|
|
|
|
|
|
|
namespace S3Demo
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Amazon;
|
|
|
|
|
using Amazon.Runtime;
|
|
|
|
|
using Amazon.S3;
|
|
|
|
|
using Amazon.S3.Model;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This example shows how to work with tags in Amazon Simple Storage
|
|
|
|
|
/// System (Amazon S3) objects. The example was created using the AWS SDK
|
|
|
|
|
/// for .NET version 3.7 and .NET Core 5.0.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ObjectTag
|
|
|
|
|
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";
|
|
|
|
@ -112,5 +109,77 @@ namespace S3Demo
|
|
|
|
|
$"Error: '{ex.Message}'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|