我正在ASP.NET CORE中编写一个系统,并将其上传到AZURE。
我正在寻找一个免费的地方来存储客户上传的文件。
Azure存储需要花钱,所以我想把它连接到谷歌硬盘有可能吗?你能向我解释一下怎么做吗?
您可以挑衅地将文件存储在Google驱动器中。使用谷歌驱动api,使用服务帐户进行操作,因为听起来你只会上传到你控制的帐户。
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Service account Authentication Sample",
});
使用服务帐户将文件上传到Google Drive API
至于在azure部分的主机。我建议使用json密钥文件,而不是p12证书文件。我以前在azure中遇到过这样的问题。
从内存上传
由于用户上传的文件可能只是内存流,而不是存储在硬盘上的文件,因此使用MemoryStream 也有可能
var uploadString = "Test";
var fileName = "ploadFileString.txt";
// Upload file Metadata
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = fileName,
Parents = new List<string>() { "1R_QjyKyvET838G6loFSRu27C-3ASMJJa" } // folder to upload the file to
};
var fsSource = new MemoryStream(Encoding.UTF8.GetBytes(uploadString ?? ""));
string uploadedFileId;
// Create a new file, with metadata and stream.
var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
request.Fields = "*";
var results = await request.UploadAsync(CancellationToken.None);
if (results.Status == UploadStatus.Failed)
{
Console.WriteLine($"Error uploading file: {results.Exception.Message}");
}
// the file id of the new file we created
uploadedFileId = request.ResponseBody?.Id;
如何使用C#从内存上传到Google Drive API