Sinh_R
New member
- Joined
- Jun 26, 2022
- Messages
- 2
- Programming Experience
- Beginner
Hi,
I need some guidance regarding the problem I am facing.
PROBLEM:
I am currently working on MinIO object storage (similar to Azure Blob and AWS S3) and I am currently trying to implement a method which will download the files uploaded in my local cloud storage.
For that I want to create a Class which will has 3 properties:
After this now I decided to create a method which will be called to download the object. This will only use one argument which will be the name of the file to be downloaded from the local cloud storage.
For this I decided to create a method which will be of generic type and came with some thing like this:
Now the main problem is that I am not able to return the object of type MemoryStream. If I tried this on line no 28
This is giving error saying cannot convert MemoryStream to T and doing that explicitly might be wrong.
So I need help on what can be done here?
Guidance in any way will be appreciated.
I need some guidance regarding the problem I am facing.
PROBLEM:
I am currently working on MinIO object storage (similar to Azure Blob and AWS S3) and I am currently trying to implement a method which will download the files uploaded in my local cloud storage.
For that I want to create a Class which will has 3 properties:
- StatusCode: To return the status code (http codes)
- Message: A brief regarding the status of the request
- Content: This will be either of type MemoryStream or byte array and will have the actual content of the file. If fails then it will be null.
Class to download the object:
public class GetObjectResponse<T>
{
public int StatusCode { get; set; }
public string StatusMessage { get; set; }
public T? Response { get; set; }
}
After this now I decided to create a method which will be called to download the object. This will only use one argument which will be the name of the file to be downloaded from the local cloud storage.
For this I decided to create a method which will be of generic type and came with some thing like this:
Method to download object:
public async Task<GetObjectResponse<T>> GetObject<T>(string objectName)
{
try
{
var connString = _minioConfig.Value.ConnectionString;
var bucketName = _minioConfig.Value.BucketName;
var client = CreateClient(connString);
var downloadStream = new MemoryStream();
if (!await client.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName)))
{
throw new BucketNotFoundException(bucketName, "Bucket does not exists");
}
await client.GetObjectAsync
(
new GetObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithCallbackStream(stream =>
{
stream.CopyTo(downloadStream);
})
);
//If T is of type MemoryStream return GetObjectResponse<MemoryStream>
//Else GetObjectResponse<byte[]>
}
catch (MinioException mex)
{
return new GetObjectResponse<T>
{
StatusCode = 400,
StatusMessage = mex.Message,
Response = default
};
}
catch (Exception ex)
{
return new GetObjectResponse<T>
{
StatusCode = 400,
StatusMessage = ex.Message,
Response = default
};
}
}
Now the main problem is that I am not able to return the object of type MemoryStream. If I tried this on line no 28
C#:
return new GetObjectResponse<MemoryStream>
{
//setting the properties
}
This is giving error saying cannot convert MemoryStream to T and doing that explicitly might be wrong.
So I need help on what can be done here?
Guidance in any way will be appreciated.