diff --git a/Directory.Packages.props b/Directory.Packages.props index 667658e..b63cc5e 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -20,5 +20,6 @@ + - \ No newline at end of file + diff --git a/sample/AliyunCredentialsUsage/AliyunCredentialsUsage.csproj b/sample/AliyunCredentialsUsage/AliyunCredentialsUsage.csproj new file mode 100644 index 0000000..84753e5 --- /dev/null +++ b/sample/AliyunCredentialsUsage/AliyunCredentialsUsage.csproj @@ -0,0 +1,14 @@ + + + + + + + + + + + + $(NoWarn);CS8002 + + diff --git a/sample/AliyunCredentialsUsage/Program.cs b/sample/AliyunCredentialsUsage/Program.cs new file mode 100644 index 0000000..970f4bd --- /dev/null +++ b/sample/AliyunCredentialsUsage/Program.cs @@ -0,0 +1,92 @@ +using CommandLine; +using OSS = AlibabaCloud.OSS.V2; + +namespace Sample.AliyunCredentialsUsage +{ + public class Program + { + + public class Options + { + [Option("region", Required = true, HelpText = "The region in which the bucket is located.")] + public string? Region { get; set; } + + [Option("endpoint", Required = false, HelpText = "The domain names that other services can use to access OSS.")] + public string? Endpoint { get; set; } + } + + public static async Task Main(string[] args) + { + + var parserResult = Parser.Default.ParseArguments(args); + if (parserResult.Errors.Any()) + { + Environment.Exit(1); + } + var option = parserResult.Value; + + // Specify the region and other parameters. + var region = option.Region; + var endpoint = option.Endpoint; + + // Aliyun Credentials + // Take RamRoleArn for example + // For more examples, please refer to https://github.com/aliyun/credentials-csharp. + var credConfig = new Aliyun.Credentials.Models.Config() + { + // Which type of credential you want + Type = "ram_role_arn", + // AccessKeyId of your account + AccessKeyId = "", + // AccessKeySecret of your account + AccessKeySecret = "", + // Format: acs:ram::USER_Id:role/ROLE_NAME + // RoleArn can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN + RoleArn = "", + // Role Session Name + RoleSessionName = "", + // Optional, limit the permissions of STS Token + Policy = "", + // Optional, limit the Valid time of STS Token + RoleSessionExpiration = 3600, + }; + var credClient = new Aliyun.Credentials.Client(credConfig); + + // Cast to OSS Credentials Provider + var credentialsProvider = new OSS.Credentials.CredentialsProvideFunc(() => + { + var credential = credClient.GetCredential(); + return new OSS.Credentials.Credentials( + credential.AccessKeyId, + credential.AccessKeySecret, + credential.SecurityToken); + }); + + // Using the SDK's default configuration + // loading credentials values from the environment variables + var cfg = OSS.Configuration.LoadDefault(); + cfg.Region = region; + cfg.CredentialsProvider = credentialsProvider; + + if (endpoint != null) + { + cfg.Endpoint = endpoint; + } + + using var client = new OSS.Client(cfg); + + // Create the Paginator for the ListBuckets operation. + var paginator = client.ListBucketsPaginator(new OSS.Models.ListBucketsRequest()); + + // Iterate through the bucket pages + Console.WriteLine("Buckets:"); + await foreach (var page in paginator.IterPageAsync()) + { + foreach (var bucket in page.Buckets ?? []) + { + Console.WriteLine($"Bucket:{bucket.Name}, {bucket.StorageClass}, {bucket.Location}"); + } + } + } + } +} diff --git a/sample/alibabacloud-oss-csharp-sdk-v2-sample.sln b/sample/alibabacloud-oss-csharp-sdk-v2-sample.sln index c124607..c798d7b 100644 --- a/sample/alibabacloud-oss-csharp-sdk-v2-sample.sln +++ b/sample/alibabacloud-oss-csharp-sdk-v2-sample.sln @@ -79,6 +79,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeleteMultipleObjects", "De EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeleteBucket", "DeleteBucket\DeleteBucket.csproj", "{1CFCF2DD-C9F6-4903-A530-C7B813A6ED27}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliyunCredentialsUsage", "AliyunCredentialsUsage\AliyunCredentialsUsage.csproj", "{02A88682-C702-461A-BE45-03FE071774F1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -237,6 +239,10 @@ Global {1CFCF2DD-C9F6-4903-A530-C7B813A6ED27}.Debug|Any CPU.Build.0 = Debug|Any CPU {1CFCF2DD-C9F6-4903-A530-C7B813A6ED27}.Release|Any CPU.ActiveCfg = Release|Any CPU {1CFCF2DD-C9F6-4903-A530-C7B813A6ED27}.Release|Any CPU.Build.0 = Release|Any CPU + {02A88682-C702-461A-BE45-03FE071774F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {02A88682-C702-461A-BE45-03FE071774F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {02A88682-C702-461A-BE45-03FE071774F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {02A88682-C702-461A-BE45-03FE071774F1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE