namespace Enosis.Voltron.WebService.Services
{
public class GoogleDocumentsStorageServiceOptions
{
public string BucketName { get; set; }
}
public class GoogleDocumentsStorageService : IDocumentsStorageService
{
private StorageClient storageClient;
private readonly GoogleDocumentsStorageServiceOptions options;
public GoogleDocumentsStorageService(IOptions<GoogleDocumentsStorageServiceOptions> options)
{
this.options = options.Value;
storageClient = StorageClient.Create();
}
public async Task<GetFileResult> GetFile(string fileName)
{
var stream = new MemoryStream();
Google.Apis.Storage.v1.Data.Object metaData;
try
{
await storageClient.DownloadObjectAsync(options.BucketName, fileName, stream);
metaData = await storageClient.GetObjectAsync(options.BucketName, fileName);
}
catch (Google.GoogleApiException ex)
{
Console.WriteLine(ex);
return new GetFileResult()
{
Success = false,
ErrorHttpStatusCode = Convert.ToInt16(ex.HttpStatusCode),
ErrorMessage = ex.Message
};
}
stream.Seek(0, 0);
return new GetFileResult()
{
Success = true,
FileStream = stream,
ContentType = metaData.ContentType
};
}
public async Task<StoreFileResult> StoreFile(StoreFileRequest storeFileRequest)
{
var fileBytes = Convert.FromBase64String(storeFileRequest.FileDataBase64);
try
{
var fileObject = await storageClient.UploadObjectAsync(
bucket: options.BucketName,
objectName: storeFileRequest.FileName,
contentType: storeFileRequest.ContentType,
source: new MemoryStream(fileBytes),
options: new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.Private }
);
if (fileObject == null)
{
throw new Exception("Failed to upload file");
}
}
catch (Google.GoogleApiException ex)
{
Console.WriteLine($"Error uploading file {storeFileRequest.FileName}: {ex.Message}");
return new StoreFileResult()
{
Success = false,
ErrorMessage = ex.Message,
ErrorHttpStatusCode = ex.HttpStatusCode
};
}
return new StoreFileResult()
{
Success = true
};
}
public async Task DeleteFileAsync(string fileName)
{
try
{
await storageClient.DeleteObjectAsync(options.BucketName, fileName);
}
catch (GoogleApiException e)
{
if (e.Error == null || e.Error.Code != 404)
{
throw;
}
}
}
}
}
Exception: Object reference not set to an instance of an object. Stack Trace: at System.Object.GetType()
at [email protected](SyntaxNode node)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprIndentIfMultiline@304[a](FSharpFunc`2 errorPrinter, FSharpFunc`2 f, Expr`1 e)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprIndentIfMultiline@304[a](FSharpFunc`2 errorPrinter, FSharpFunc`2 f, Expr`1 e)
at cs2fs.FSharpOutput.getExprMNP@320[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprIndentIfMultiline@304[a](FSharpFunc`2 errorPrinter, FSharpFunc`2 f, Expr`1 e)
at cs2fs.FSharpOutput.getMatch@171[a](FSharpFunc`2 errorPrinter, a tupledArg0, Pat`1 tupledArg1, FSharpOption`1 tupledArg2, Expr`1 tupledArg3)
at [email protected](Tuple`4 m)
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@74.DoMoveNext(b& curr)
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System-Collections-IEnumerator-MoveNext()
at Microsoft.FSharp.Collections.SeqModule.ToList[T](IEnumerable`1 source)
at cs2fs.FSharpOutput.delimLineText(String sep, IEnumerable`1 xs)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprNP@319[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at [email protected](Expr`1 e)
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@74.DoMoveNext(b& curr)
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System-Collections-IEnumerator-MoveNext()
at Microsoft.FSharp.Collections.SeqModule.ToList[T](IEnumerable`1 source)
at cs2fs.FSharpOutput.delimLineText(String sep, IEnumerable`1 xs)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getMember@175[a](FSharpFunc`2 errorPrinter, String className, Expr`1 x)
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@74.DoMoveNext(b& curr)
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System-Collections-IEnumerator-MoveNext()
at Microsoft.FSharp.Collections.SeqModule.ToList[T](IEnumerable`1 source)
at cs2fs.FSharpOutput.lineblock(IEnumerable`1 xs)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprNP@319[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at [email protected](Expr`1 e)
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@74.DoMoveNext(b& curr)
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System-Collections-IEnumerator-MoveNext()
at Microsoft.FSharp.Collections.SeqModule.ToList[T](IEnumerable`1 source)
at cs2fs.FSharpOutput.delimLineText(String sep, IEnumerable`1 xs)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExprNP@319[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at [email protected](Expr`1 e)
at Microsoft.FSharp.Collections.Internal.IEnumerator.map@74.DoMoveNext(b& curr)
at Microsoft.FSharp.Collections.Internal.IEnumerator.MapEnumerator`1.System-Collections-IEnumerator-MoveNext()
at Microsoft.FSharp.Collections.SeqModule.ToList[T](IEnumerable`1 source)
at cs2fs.FSharpOutput.delimLineText(String sep, IEnumerable`1 xs)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.FSharpOutput.getExpr@218[a](FSharpFunc`2 errorPrinter, Expr`1 e)
at cs2fs.Convert.convertText(String csharp)
at Server.Result.attempt[a](FSharpFunc`2 f) in /build/src/Server/Server.fs:line 24