如何使用.NET和Google.cloud.storage.v1检查文件是否存在于Google Cloud Storage中?
我想在尝试删除文件之前检查文件是否存在,因为我想避免例外。删除方法以下 - deleteassetAsync()
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
internal sealed class AssetsService
{
private readonly GoogleCredential _googleCredential;
private readonly StorageClient _storageClient;
public AssetsService()
{
_googleCredential = GoogleCredential.FromFile("google.json");
_storageClient = StorageClient.Create(_googleCredential);
}
public async Task DeleteAssetAsync()
{
await _storageClient.DeleteObjectAsync("gcp-assets-bucket", "file.txt");
}
}
例外
google.apis.requests.requesterror没有这样的对象: GCP-Assets-bucket/file.txt [404]错误[ 消息[无这样的对象:GCP-Assets-bucket/file.txt]位置[ - ]原因[notfound]域[global]]
服务存储已引发一个例外: Google.googleapiexception:google.apis.requests.requesterror no 对象:GCP-Assets-bucket/file.txt [404]错误[ 消息[无这样的对象:GCP-Assets-bucket/file.txt]位置[ - ]原因[notfound]域[global]]
我尝试先下载或获取文件,并检查它不是null,而是下载或下载或如果找不到文件,则返回相同的异常。
getObject 示例
var googleAsset = await _storageClient.GetObjectAsync("gcp-assets-bucket", "file.txt");
if (googleAsset is not null)
{
_storageClient.DeleteObjectAsync(googleAsset);
}
using var stream = File.OpenWrite("file.txt");
await _storageClient.DownloadObjectAsync("gcp-assets-bucket", "file.txt", stream);
是否有任何方法可以检查Google Cloud Storage中是否存在文件,或者如果不抛出异常,则不存在文件?
I would like to check if the file exists before trying to delete it as I want to avoid the exception. Removal method below - DeleteAssetAsync()
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
internal sealed class AssetsService
{
private readonly GoogleCredential _googleCredential;
private readonly StorageClient _storageClient;
public AssetsService()
{
_googleCredential = GoogleCredential.FromFile("google.json");
_storageClient = StorageClient.Create(_googleCredential);
}
public async Task DeleteAssetAsync()
{
await _storageClient.DeleteObjectAsync("gcp-assets-bucket", "file.txt");
}
}
Exception
Google.Apis.Requests.RequestError No such object:
gcp-assets-bucket/file.txt [404] Errors [
Message[No such object: gcp-assets-bucket/file.txt] Location[ - ] Reason[notFound] Domain[global] ]The service storage has thrown an exception:
Google.GoogleApiException: Google.Apis.Requests.RequestError No such
object: gcp-assets-bucket/file.txt [404] Errors [
Message[No such object: gcp-assets-bucket/file.txt] Location[ - ] Reason[notFound] Domain[global] ]
I tried to download or get the file first and check if it is not null, but the downloading or getting method returns the same exception if it cannot find the file.
GetObject example
var googleAsset = await _storageClient.GetObjectAsync("gcp-assets-bucket", "file.txt");
if (googleAsset is not null)
{
_storageClient.DeleteObjectAsync(googleAsset);
}
DownloadObject example
using var stream = File.OpenWrite("file.txt");
await _storageClient.DownloadObjectAsync("gcp-assets-bucket", "file.txt", stream);
Is there any way to check if file exists in Google Cloud Storage or get null if not exists without throwing an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同样的想法约翰·汉利(John Hanley)提到。
使用ListObjects列表获取具有相同前缀(目录路径)的文件列表。然后检查对象是否存在。
Same idea John Hanley mentioned.
Use ListObjects list to get the list of files with the same prefix (directory path). Then check if the object is present.