如何使用.NET和Google.cloud.storage.v1检查文件是否存在于Google Cloud Storage中?

发布于 2025-01-19 05:56:15 字数 1564 浏览 1 评论 0原文

我想在尝试删除文件之前检查文件是否存在,因为我想避免例外。删除方法以下 - 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);
}

downloadObject 示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2025-01-26 05:56:15

同样的想法约翰·汉利(John Hanley)提到。
使用ListObjects列表获取具有相同前缀(目录路径)的文件列表。然后检查对象是否存在。

private bool FileExists(string bucketName, string fileName)
{
    var objects = _storageClient.ListObjects(bucketName, Path.GetDirectoryName(fileName)).ToArray();
    return objects.Any(x => x.Name == fileName);
}

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.

private bool FileExists(string bucketName, string fileName)
{
    var objects = _storageClient.ListObjects(bucketName, Path.GetDirectoryName(fileName)).ToArray();
    return objects.Any(x => x.Name == fileName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文