public MatrixCalculation GetCalculation(string id) { var ms = new MemoryStream(); Task.WaitAll(_client.GetObjectAsync(_bucketName, id, (e) => e.CopyTo(ms))); return(JsonConvert.DeserializeObject <MatrixCalculation>(Encoding.UTF8.GetString(ms.ToArray()))); }
/// <summary> /// 获取文件 /// </summary> /// <param name="bucketName"></param> /// <param name="objectName"></param> /// <param name="callback"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task GetObjectAsync(string bucketName, string objectName, Action <Stream> callback, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } await _client.GetObjectAsync(bucketName, objectName, (stream) => { callback(stream); }, null, cancellationToken); }
private async static Task CopyObject_Test1(MinioClient minio) { Console.Out.WriteLine("Test1: CopyObjectsAsync"); string bucketName = GetRandomName(15); string objectName = GetRandomName(10); string destBucketName = GetRandomName(15); string destObjectName = GetRandomName(10); string fileName = CreateFile(1 * MB); await Setup_Test(minio, bucketName); await Setup_Test(minio, destBucketName); await minio.PutObjectAsync(bucketName, objectName, fileName); await minio.CopyObjectAsync(bucketName, objectName, destBucketName, destObjectName); string outFileName = "outFileName"; await minio.GetObjectAsync(destBucketName, destObjectName, outFileName); File.Delete(outFileName); await minio.RemoveObjectAsync(bucketName, objectName); await minio.RemoveObjectAsync(destBucketName, destObjectName); await TearDown(minio, bucketName); await TearDown(minio, destBucketName); File.Delete(fileName); Console.Out.WriteLine("Test1: CopyObjectsAsync Complete"); }
public async Task <string> RestoreLatestBackupToTempLocation(CancellationToken cancellationToken) { Trace.TraceInformation("MinioBackupManager: Download backup async called."); var lastBackupBlob = (await this.GetBackupBlobs(true)).First(); Trace.TraceInformation("MinioBackupManager: Downloading {0}", lastBackupBlob.Key); string downloadId = Guid.NewGuid().ToString("N"); string zipPath = Path.Combine(this.PartitionTempDirectory, string.Format("{0}_Backup.zip", downloadId)); await EnsureBucket(); await minio.GetObjectAsync(bucketName, lastBackupBlob.Key, zipPath); string restorePath = Path.Combine(this.PartitionTempDirectory, downloadId); ZipFile.ExtractToDirectory(zipPath, restorePath); FileInfo zipInfo = new FileInfo(zipPath); zipInfo.Delete(); Trace.TraceInformation("MinioBackupManager: Downloaded {0} in to {1}", lastBackupBlob.Key, restorePath); return(restorePath); }
/// <summary> /// 获取文件 /// </summary> /// <param name="objectName"></param> /// <param name="filePath"></param> /// <param name="cancellationToken"></param> /// <param name="bucketName"></param> /// <returns></returns> public async Task GetObjectAsync(string objectName, string filePath, CancellationToken cancellationToken = default, string bucketName = default) { if (bucketName.IsNull()) { bucketName = _config.BucketName; } CheckParams(bucketName, objectName); string dir = Path.GetDirectoryName(filePath); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) { Directory.CreateDirectory(dir); } try { // 创建OssClient实例。 var client = new MinioClient(_config.EndPoint, _config.AccessKey, _config.SecretKey); await client.GetObjectAsync(bucketName, objectName, filePath, null, cancellationToken); } catch (Exception ex) { _logger.LogError("MinIO OSS获取文件异常:{@ex}", ex); throw ex; } }
// Download object from bucket into local file public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "local-filename", ServerSideEncryption sse = null) { try { Console.WriteLine("Running example for API: GetObjectAsync"); File.Delete(fileName); GetObjectArgs args = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFile(fileName) .WithServerSideEncryption(sse); await minio.GetObjectAsync(args).ConfigureAwait(false); Console.WriteLine($"Downloaded the file {fileName} from bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
public async Task <IActionResult> DownloadUploadFile(string FileName) { FileStreamResult fileStreamResult = null; try { await Task.Run(async() => { bool found = await minio.BucketExistsAsync(bucketName); if (found) { //如果找不到对象,将引发异常, await minio.StatObjectAsync(bucketName, FileName); //从桶中获取对象的流 await minio.GetObjectAsync(bucketName, FileName, (stream) => { fileStreamResult = File(stream, "application/octet-stream"); }); } } ); return(fileStreamResult); } catch (MinioException e) { Console.Out.WriteLine("Error occurred: " + e); } return(fileStreamResult); }
public async Task TestInvalidObjectNameError() { var badName = new string('A', 260); var bucketName = Guid.NewGuid().ToString("N"); var minio = new MinioClient("play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); try { await minio.MakeBucketAsync(bucketName); var ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>( () => minio.StatObjectAsync(bucketName, badName)); Assert.AreEqual(ex.Response.Code, "InvalidObjectName"); ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>( () => minio.GetObjectAsync(bucketName, badName, s => { })); Assert.AreEqual(ex.Response.Code, "InvalidObjectName"); } finally { await minio.RemoveBucketAsync(bucketName); } }
// Get object in a bucket public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string versionId = "my-version-id", string fileName = "my-file-name") { try { if (string.IsNullOrEmpty(versionId)) { Console.WriteLine("Version ID is empty or not assigned"); return; } Console.WriteLine("Running example for API: GetObjectAsync with Version ID"); GetObjectArgs args = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithVersionId(versionId) .WithFile(fileName); await minio.GetObjectAsync(args); Console.WriteLine($"Downloaded the file {fileName} for object {objectName} with version {versionId} in bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
public T Get(string key) { try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { //lock (_lockconnections) //{ client.GetObjectAsync(this.Bucket, fixKey(key), m => m.CopyTo(ms)).Wait(); //} if (typeof(T) == typeof(byte[])) { return(ms.ToArray() as T); } else { string json = Encoding.UTF8.GetString(ms.ToArray()); return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(json)); } } } catch (Exception e) { ESTraceLogger.Error($"AWS_S3CacheProvider: get record {fixKey(key)} error {e.Message}", e); return(null); } }
public async Task Get(string objectName, Action <Stream> reader, CancellationToken cancellationToken = default) { var tsc = new TaskCompletionSource <Unit>(); await _client.GetObjectAsync(_bucketName, objectName, async s => { try { if (cancellationToken.IsCancellationRequested) { tsc.SetCanceled(); return; } reader(s); if (cancellationToken.IsCancellationRequested) { tsc.SetCanceled(); return; } tsc.SetResult(Unit.Default); } catch (Exception ex) { tsc.SetException(ex); } }, cancellationToken : cancellationToken); await tsc.Task; }
public async Task TestRetryPolicyOnFailure() { var client = new MinioClient("play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG").WithSSL(); int invokeCount = 0; var retryCount = 3; client.WithRetryPolicy( async(callback) => { Exception exception = null; for (int i = 0; i < retryCount; i++) { invokeCount++; try { return(await callback()); } catch (BucketNotFoundException ex) { exception = ex; } } throw exception; }); await Assert.ThrowsExceptionAsync <BucketNotFoundException>( () => client.GetObjectAsync(Guid.NewGuid().ToString(), "aa", s => { })); Assert.AreEqual(invokeCount, retryCount); }
// Get object in a bucket for a particular offset range. Dotnet SDK currently // requires both start offset and end public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "my-file-name") { try { Console.Out.WriteLine("Running example for API: GetObjectAsync"); // Check whether the object exists using StatObjectAsync(). If the object is not found, // StatObjectAsync() will throw an exception. await minio.StatObjectAsync(bucketName, objectName); // Get object content starting at byte position 1024 and length of 4096 await minio.GetObjectAsync(bucketName, objectName, 1024L, 4096L, (stream) => { var fileStream = File.Create(fileName); stream.CopyTo(fileStream); fileStream.Dispose(); FileInfo writtenInfo = new FileInfo(fileName); long file_read_size = writtenInfo.Length; // Uncommment to print the file on output console // stream.CopyTo(Console.OpenStandardOutput()); Console.WriteLine("Successfully downloaded object with requested offset and length {0} into file", writtenInfo.Length); stream.Dispose(); }); Console.Out.WriteLine(); } catch (Exception e) { Console.WriteLine("[Bucket] Exception: {0}", e); } }
/// <inheritdoc /> /// <summary> /// Retrieves a single file from the specified bucket in Minio. /// </summary> /// <param name="fileName">A string, name fo the file which is stored.</param> /// <returns>A Task, of type stream which contains the content of the file retrieved.</returns> public async Task <Stream> RetrieveFile(string fileName) { Log.Information($"Retrieving file: {fileName}, from bucket: {_configurationSettings.Value.BucketName}."); Stream returnStream = null; // Check whether the object exists using statObject(). // If the object is not found, statObject() throws an exception, // else it means that the object exists. // Execution is successful. try { await _minioClient.StatObjectAsync(_configurationSettings.Value.BucketName, fileName); } catch (Exception exception) { if (exception.Message.Contains("Not found")) { return(null); } } // Get input stream to have content of 'fileName' from 'bucketName' await _minioClient.GetObjectAsync(_configurationSettings.Value.BucketName, fileName, (stream) => { stream.CopyTo(returnStream); }); returnStream.Seek(0, SeekOrigin.Begin); Log.Information("Successfully retrieved file."); return(returnStream); }
async Task <string> GetJournalContent(UserJournal journalItem) { if (journalItem == null) { return(null); } using (MemoryStream outFile = new MemoryStream()) { var journalIdentifier = journalItem.S3Path; try { var stats = await _minioClient.StatObjectAsync("journal-limpet", journalIdentifier); await _minioClient.GetObjectAsync("journal-limpet", journalIdentifier, 0, stats.Size, cb => { cb.CopyTo(outFile); } ); outFile.Seek(0, SeekOrigin.Begin); var journalContent = ZipManager.Unzip(outFile.ToArray()); return(journalContent); } catch (ObjectNotFoundException) { return(null); } } }
public async Task <ImageInfo> GetAsync(string fileName) { await using var memoryStream = new MemoryStream(); var extension = Path.GetExtension(fileName); if (string.IsNullOrWhiteSpace(extension)) { throw new ArgumentException("File extension is empty"); } var exists = await BlobExistsAsync(fileName); if (!exists) { _logger.LogWarning($"Blob {fileName} not exist."); return(null); } await _client.GetObjectAsync(_bucketName, fileName, stream => { stream?.CopyTo(memoryStream); }); var arr = memoryStream.ToArray(); var fileType = extension.Replace(".", string.Empty); var imageInfo = new ImageInfo { ImageBytes = arr, ImageExtensionName = fileType }; return(imageInfo); }
// Get object in a bucket public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string versionId = "my-version-id", string fileName = "my-file-name", string matchEtag = null, DateTime modifiedSince = default(DateTime)) { try { string withVersionId = (string.IsNullOrEmpty(versionId))?"":" with Version ID"; Console.WriteLine("Running example for API: GetObjectAsync" + withVersionId); GetObjectArgs args = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithVersionId(versionId) .WithFile(fileName) .WithMatchETag(matchEtag) .WithModifiedSince(modifiedSince); await minio.GetObjectAsync(args); Console.WriteLine($"Downloaded the file {fileName} for object {objectName} with given query parameters in bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
public Task GetObjectAsync(string bucketName, string objectName, Action <Stream> callback, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); GetObjectArgs args = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithCallbackStream((stream) => { callback(stream); }); return(_client.GetObjectAsync(args, cancellationToken)); }
private async static Task PutObject_Tester(MinioClient minio, string bucketName, string objectName, string fileName = null, string contentType = "application/octet-stream", long size = 0) { try { byte[] bs = File.ReadAllBytes(fileName); System.IO.MemoryStream filestream = new System.IO.MemoryStream(bs); long file_write_size = filestream.Length; long file_read_size = 0; string tempFileName = "tempfiletosavestream"; if (size == 0) { size = filestream.Length; } if (filestream.Length < (5 * MB)) { Console.Out.WriteLine("Test1: PutobjectAsync: PutObjectAsync with Stream"); } else { Console.Out.WriteLine("Test1: PutobjectAsync: PutObjectAsync with Stream and MultiPartUpload"); } await minio.PutObjectAsync(bucketName, objectName, filestream, size, contentType); await minio.GetObjectAsync(bucketName, objectName, (stream) => { var fileStream = File.Create(tempFileName); stream.CopyTo(fileStream); fileStream.Dispose(); FileInfo writtenInfo = new FileInfo(tempFileName); file_read_size = writtenInfo.Length; Assert.AreEqual(file_read_size, file_write_size); File.Delete(tempFileName); }); ObjectStat statObject = await minio.StatObjectAsync(bucketName, objectName); Assert.IsNotNull(statObject); Assert.AreEqual(statObject.ObjectName, objectName); Assert.AreEqual(statObject.Size, file_read_size); Assert.AreEqual(statObject.ContentType, contentType); await minio.RemoveObjectAsync(bucketName, objectName); } catch (Exception e) { Console.WriteLine("[Bucket] Exception: {0}", e); Assert.Fail(); } }
/// <summary> /// 下载特定对象为流 /// </summary> /// <param name="bucketName">桶名称</param> /// <param name="objectName">对象名称</param> /// <returns></returns> public async Task <Stream> GetObjectAsync(string bucketName, string objectName) { try { Stream outStream = null; await _minioClient.StatObjectAsync(bucketName, objectName); await _minioClient.GetObjectAsync(bucketName, objectName, (stream) => { outStream = stream; }); return(outStream); } catch (Exception ex) { throw ex; } }
public async Task <byte[]> GetBlob(string bucketName, string blobName) { using (var ms = new MemoryStream()) { return(await _client.GetObjectAsync(bucketName, blobName, (stream) => { stream.CopyTo(ms); }).ContinueWith(tsk => ms.ToArray())); } }
// TODO: OBJECT OPERATIONS //Downloads an object as a stream. public Task GetObjectAsStreamAsync(string bucketName, string objectName, Action <Stream> callback, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken)) { try { // Check whether the object exists using statObject(). // If the object is not found, statObject() throws an exception, // else it means that the object exists. // Execution is successful. var taskStatObject = minio.StatObjectAsync(bucketName, objectName); Task.WaitAll(taskStatObject); // Get input stream to have content of 'my-objectname' from 'my-bucketname' var taskGetObject = minio.GetObjectAsync(bucketName, objectName, callback, sse, cancellationToken); return(taskGetObject); } catch (MinioException e) { throw; } }
public async Task <byte[]> GetObject(long id) { var model = _context.ReportPdfBucket.Find(id); var result = new byte[] { }; await _minioClient.GetObjectAsync(BUCKET_NAME, $"{model.HashIdEmployee}/{model.FileName}{EXTENSION_FILE}", stream => { result = stream.ReadAsBytes(); }); return(result); }
private static void ReceiveMessage(ConnectionFactory factory) { using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "URL", durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += async(model, ea) => { var body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); var split = message.Split(' '); var urlParseResult = split[0].Split('/'); string bucketName = urlParseResult[3]; string fileName = urlParseResult[4].Split('?')[0]; var filePath = "Incoming/" + fileName; //Extract incoming file path if exists if (split.Length > 1) { filePath = split[1]; } Console.WriteLine("Message Received!"); var minioClient = new MinioClient(MinIOEndpoint, MinIOAccessKey, MinIOSecretKey ).WithSSL(); // Check whether the object exists using statObject(). await minioClient.StatObjectAsync(bucketName, fileName); var exePath = Path.GetDirectoryName(System.Reflection .Assembly.GetExecutingAssembly().CodeBase); using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create)) { // Get input stream to have content of 'my-objectname' from 'my-bucketname' await minioClient.GetObjectAsync(bucketName, fileName, (stream) => { stream.CopyTo(outputFileStream); }); } }; channel.BasicConsume(queue: "URL", autoAck: true, consumer: consumer); Console.WriteLine(" Listening to the Messages!"); Thread.Sleep(10); } }
public async Task <byte[]> GetAsync(string bucketName, string objectName) { byte[] result = null; await _client.GetObjectAsync(bucketName, objectName, stream => { result = stream.ReadAsBytes(); }); return(result); }
private async Task <(string, string)> GetInputDataFromBucket(MinioClient client, string calcUuid, CancellationToken cancelToken) { var vxString = string.Empty; var ecnString = string.Empty; var vxFileNameInBucket = PumpConsts.VxFilePrefix + calcUuid + PumpConsts.DataFileExtension; var ecnFileNameInBucket = PumpConsts.EcnFilePrefix + calcUuid + PumpConsts.DataFileExtension; await Task.WhenAll( client.GetObjectAsync(PumpConsts.MinioBucketName, vxFileNameInBucket, x => { vxString = GetString(x); }, cancellationToken: cancelToken), client.GetObjectAsync(PumpConsts.MinioBucketName, ecnFileNameInBucket, x => { ecnString = GetString(x); }, cancellationToken: cancelToken) ); return(vxString, ecnString); }
private async static Task CopyObject_Test3(MinioClient minio) { Console.Out.WriteLine("Test3: CopyObjectsAsync"); // Test CopyConditions where matching ETag is found string bucketName = GetRandomName(15); string objectName = GetRandomName(10); string destBucketName = GetRandomName(15); string destObjectName = GetRandomName(10); string fileName = CreateFile(1 * MB); await Setup_Test(minio, bucketName); await Setup_Test(minio, destBucketName); await minio.PutObjectAsync(bucketName, objectName, fileName); ObjectStat stats = await minio.StatObjectAsync(bucketName, objectName); CopyConditions conditions = new CopyConditions(); conditions.SetMatchETag(stats.ETag); try { await minio.CopyObjectAsync(bucketName, objectName, destBucketName, destObjectName, conditions); } catch (MinioException) { Assert.Fail(); } string outFileName = "outFileName"; ObjectStat dstats = await minio.StatObjectAsync(destBucketName, destObjectName); Assert.IsNotNull(dstats); Assert.AreEqual(dstats.ETag, stats.ETag); Assert.AreEqual(dstats.ObjectName, destObjectName); await minio.GetObjectAsync(destBucketName, destObjectName, outFileName); File.Delete(outFileName); await minio.RemoveObjectAsync(bucketName, objectName); await minio.RemoveObjectAsync(destBucketName, destObjectName); await TearDown(minio, bucketName); await TearDown(minio, destBucketName); File.Delete(fileName); Console.Out.WriteLine("Test3: CopyObjectsAsync Complete"); }
private async Task <string> GetObject(string objectName, CancellationToken cancellationToken) { var json = ""; await _client.GetObjectAsync(_bucket, objectName, stream => { using (var streamReader = new StreamReader(stream)) { json = streamReader.ReadToEnd(); } }, cancellationToken); return(json); }
public async Task <IActionResult> GetAttachementFile( [FromRoute, EmailAddress, Required] string receiverAddress, [FromRoute, Required] Guid?fileGuid) { var memoryStream = new MemoryStream(); await _minio.GetObjectAsync("test", "Exercise-2-Container.zip", stream => { stream.CopyTo(memoryStream); }); memoryStream.Position = 0; return(File(memoryStream, "application/octet-stream", "test.zip")); }
private async Task <bool> ObjectExistsAsync(MinioClient client, string bucket, string objectName) { try { await client.GetObjectAsync("bucket", objectName, stream => { }); return(true); } catch (ObjectNotFoundException e) { return(false); } }