// Establish Credentials with AWS IAM Credentials in Environmental variables public async static Task Run() { AWSEnvironmentProvider provider = new AWSEnvironmentProvider(); MinioClient minioClient = new MinioClient() .WithEndpoint("s3.amazonaws.com") .WithSSL() .WithCredentialsProvider(provider) .Build(); try { StatObjectArgs statObjectArgs = new StatObjectArgs() .WithBucket("my-bucket-name") .WithObject("my-object-name"); ObjectStat result = await minioClient.StatObjectAsync(statObjectArgs); Console.WriteLine("Object Stat: \n" + result.ToString()); } catch (MinioException me) { Console.WriteLine($"[Bucket] IAMAWSProviderExample example case encountered Exception: {me}"); } catch (Exception e) { Console.WriteLine($"[Bucket] IAMAWSProviderExample example case encountered Exception: {e}"); } }
// Establish Credentials with AWS Session token public async static Task Run() { ChainedProvider provider = new ChainedProvider() .AddProviders(new ClientProvider[] { new AWSEnvironmentProvider(), new MinioEnvironmentProvider() }); //Chained provider definition here. MinioClient minioClient = new MinioClient() .WithEndpoint("s3.amazonaws.com") .WithSSL() .WithCredentialsProvider(provider) .Build(); try { StatObjectArgs statObjectArgs = new StatObjectArgs() .WithBucket("my-bucket-name") .WithObject("my-object-name"); ObjectStat result = await minioClient.StatObjectAsync(statObjectArgs); } catch (MinioException me) { Console.WriteLine($"[Bucket] ChainedCredentialProvider example case encountered Exception: {me}"); } catch (Exception e) { Console.WriteLine($"[Bucket] ChainedCredentialProvider example case encountered Exception: {e}"); } }
// Get stats on a object public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string bucketObject = "my-object-name", string versionID = null, string matchEtag = null, DateTime modifiedSince = default(DateTime)) { try { Console.WriteLine("Running example for API: StatObjectAsync [with ObjectQuery]"); StatObjectArgs args = new StatObjectArgs() .WithBucket(bucketName) .WithObject(bucketObject) .WithVersionId(versionID) .WithMatchETag(matchEtag) .WithModifiedSince(modifiedSince); ObjectStat statObjectVersion = await minio.StatObjectAsync(args); PrintStat(bucketObject, statObjectVersion); } catch (MinioException me) { string objectNameInfo = $"{bucketName}-{bucketObject}"; if (!string.IsNullOrEmpty(versionID)) { objectNameInfo = objectNameInfo + $" (Version ID) {me.Response.VersionId} (Marked DEL) {me.Response.DeleteMarker}"; } Console.WriteLine($"[StatObject] {objectNameInfo} Exception: {me}"); } catch (Exception e) { Console.WriteLine($"[StatObject] Exception: {e}"); } }
public async Task <ItemMeta> GetObjectMetadataAsync(string bucketName , string objectName , string versionID = null , string matchEtag = null , DateTime?modifiedSince = null) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); StatObjectArgs args = new StatObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithVersionId(versionID) .WithMatchETag(matchEtag); if (modifiedSince.HasValue) { args = args.WithModifiedSince(modifiedSince.Value); } ObjectStat statObject = await _client.StatObjectAsync(args); return(new ItemMeta() { ObjectName = statObject.ObjectName, Size = statObject.Size, LastModified = statObject.LastModified, ETag = statObject.ETag, ContentType = statObject.ContentType, IsEnableHttps = Options.IsEnableHttps, MetaData = statObject.MetaData }); }
/// <summary> /// private helper method to remove list of objects from bucket /// </summary> /// <param name="args">GetObjectArgs Arguments Object encapsulates information like - bucket name, object name etc </param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> private async Task <ObjectStat> getObjectHelper(GetObjectArgs args, CancellationToken cancellationToken = default) { // StatObject is called to both verify the existence of the object and return it with GetObject. // NOTE: This avoids writing the error body to the action stream passed (Do not remove). var statArgs = new StatObjectArgs() .WithBucket(args.BucketName) .WithObject(args.ObjectName) .WithVersionId(args.VersionId) .WithMatchETag(args.MatchETag) .WithNotMatchETag(args.NotMatchETag) .WithModifiedSince(args.ModifiedSince) .WithUnModifiedSince(args.UnModifiedSince) .WithServerSideEncryption(args.SSE); if (args.OffsetLengthSet) { statArgs.WithOffsetAndLength(args.ObjectOffset, args.ObjectLength); } var objStat = await StatObjectAsync(statArgs, cancellationToken).ConfigureAwait(false); args.Validate(); if (args.FileName != null) { await getObjectFileAsync(args, objStat, cancellationToken); } else { await getObjectStreamAsync(args, objStat, args.CallBack, cancellationToken); } return(objStat); }
// Establish Credentials with AWS IAM Credentials public static async Task Run() { var provider = new IAMAWSProvider(); var minioClient = new MinioClient() .WithEndpoint("s3.amazonaws.com") .WithSSL() .WithCredentialsProvider(provider) .WithRegion("us-west-2") .Build(); provider = provider.WithMinioClient(minioClient); try { var statObjectArgs = new StatObjectArgs() .WithBucket("my-bucket-name") .WithObject("my-object-name"); var result = await minioClient.StatObjectAsync(statObjectArgs); Console.WriteLine("Object Stat: \n" + result); } catch (MinioException me) { Console.WriteLine($"[Bucket] IAMAWSProviderExample example case encountered Exception: {me}"); } catch (Exception e) { Console.WriteLine($"[Bucket] IAMAWSProviderExample example case encountered Exception: {e}"); } }
internal StatObjectResponse(HttpStatusCode statusCode, string responseContent, Dictionary <string, string> responseHeaders, StatObjectArgs args) : base(statusCode, responseContent) { // StatObjectResponse object is populated with available stats from the response. ObjectInfo = ObjectStat.FromResponseHeaders(args.ObjectName, responseHeaders); }
public async Task TestInvalidObjectNameError() { var badName = new string('A', 260); var bucketName = Guid.NewGuid().ToString("N"); var minio = new MinioClient() .WithEndpoint("play.min.io") .WithCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG") .Build(); try { const int tryCount = 5; var mkBktArgs = new MakeBucketArgs() .WithBucket(bucketName); await minio.MakeBucketAsync(mkBktArgs); var statObjArgs = new StatObjectArgs() .WithBucket(bucketName) .WithObject(badName); var ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>( () => minio.StatObjectAsync(statObjArgs)); for (int i = 0; i < tryCount && (ex.ServerResponse != null && ex.ServerResponse.StatusCode.Equals(HttpStatusCode.ServiceUnavailable)); ++i) { ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>( () => minio.StatObjectAsync(statObjArgs)); } Assert.AreEqual(ex.Response.Code, "InvalidObjectName"); GetObjectArgs getObjectArgs = new GetObjectArgs() .WithBucket(bucketName) .WithObject(badName) .WithCallbackStream(s => {}); ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>( () => minio.GetObjectAsync(getObjectArgs)); Assert.AreEqual(ex.Response.Code, "InvalidObjectName"); } finally { var args = new RemoveBucketArgs() .WithBucket(bucketName); await minio.RemoveBucketAsync(args); } }
// Get stats on a object public static async Task Run(MinioClient minio, string bucketName = "my-bucket-name", string bucketObject = "my-object-name", string versionID = null) { try { Console.WriteLine("Running example for API: StatObjectAsync"); if (string.IsNullOrEmpty(versionID)) { var objectStatArgs = new StatObjectArgs() .WithBucket(bucketName) .WithObject(bucketObject); var statObject = await minio.StatObjectAsync(objectStatArgs); PrintStat(bucketObject, statObject); PrintMetaData(statObject.MetaData); return; } var args = new StatObjectArgs() .WithBucket(bucketName) .WithObject(bucketObject) .WithVersionId(versionID); var statObjectVersion = await minio.StatObjectAsync(args); PrintStat(bucketObject, statObjectVersion); PrintMetaData(statObjectVersion.MetaData); } catch (MinioException me) { var objectNameInfo = $"{bucketName}-{bucketObject}"; if (!string.IsNullOrEmpty(versionID)) { objectNameInfo = objectNameInfo + $" (Version ID) {me.Response.VersionId} (Delete Marker) {me.Response.DeleteMarker}"; } Console.WriteLine($"[StatObject] {objectNameInfo} Exception: {me}"); } catch (Exception e) { Console.WriteLine($"[StatObject] {bucketName}-{bucketObject} Exception: {e}"); } }
// Establish Authentication by assuming the role of an existing user public static async Task Run() { // endpoint usually point to MinIO server. var endpoint = "alias:port"; // Access key to fetch credentials from STS endpoint. var accessKey = "access-key"; // Secret key to fetch credentials from STS endpoint. var secretKey = "secret-key"; var minio = new MinioClient() .WithEndpoint(endpoint) .WithCredentials(accessKey, secretKey) .WithSSL() .Build(); try { var provider = new AssumeRoleProvider(minio); var token = await provider.GetCredentialsAsync(); // Console.WriteLine("\nToken = "); utils.Print(token); var minioClient = new MinioClient() .WithEndpoint(endpoint) .WithCredentials(token.AccessKey, token.SecretKey) .WithSessionToken(token.SessionToken) .WithSSL() .Build() ; var statObjectArgs = new StatObjectArgs() .WithBucket("bucket-name") .WithObject("object-name"); var result = await minio.StatObjectAsync(statObjectArgs); // Console.WriteLine("Object Stat: \n"); utils.Print(result); Console.WriteLine("AssumeRoleProvider test PASSed\n"); } catch (Exception e) { Console.WriteLine($"AssumeRoleProvider test exception: {e}\n"); } }
// Get object in a bucket for a particular offset range. Dotnet SDK currently // requires both start offset and end public static async Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "my-file-name") { try { Console.WriteLine("Running example for API: GetObjectAsync"); // Check whether the object exists using StatObjectAsync(). If the object is not found, // StatObjectAsync() will throw an exception. var statObjectArgs = new StatObjectArgs() .WithBucket(bucketName) .WithObject(objectName); await minio.StatObjectAsync(statObjectArgs); // Get object content starting at byte position 1024 and length of 4096 var getObjectArgs = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithOffsetAndLength(1024L, 4096L) .WithCallbackStream(stream => { var fileStream = File.Create(fileName); stream.CopyTo(fileStream); fileStream.Dispose(); var writtenInfo = new FileInfo(fileName); var file_read_size = writtenInfo.Length; // Uncomment to print the file on output console // stream.CopyTo(Console.OpenStandardOutput()); Console.WriteLine( $"Successfully downloaded object with requested offset and length {writtenInfo.Length} into file"); stream.Dispose(); }); await minio.GetObjectAsync(getObjectArgs).ConfigureAwait(false); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
// Establish Authentication on both ways with client and server certificates public static async Task Run() { // STS endpoint var stsEndpoint = "https://alias:port/"; // Generatng pfx cert for this call. // openssl pkcs12 -export -out client.pfx -inkey client.key -in client.crt -certfile server.crt using (var cert = new X509Certificate2("C:\\dev\\client.pfx", "optional-password")) { try { var provider = new CertificateIdentityProvider() .WithStsEndpoint(stsEndpoint) .WithCertificate(cert) .Build(); var minioClient = new MinioClient() .WithEndpoint("alias:port") .WithSSL() .WithCredentialsProvider(provider) .Build(); var statObjectArgs = new StatObjectArgs() .WithBucket("bucket-name") .WithObject("object-name"); var result = await minioClient.StatObjectAsync(statObjectArgs); // Console.WriteLine("\nObject Stat: \n" + result.ToString()); Console.WriteLine("\nCertificateIdentityProvider test PASSed\n"); } catch (Exception e) { Console.WriteLine($"\nCertificateIdentityProvider test exception: {e}\n"); } } }