且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查S3对象是否存在的***方法是什么?

更新时间:2022-05-07 22:31:22

您可以使用S3FileInfo类和此类的Exists方法,它会帮助您检查文件是否存在而不下载文件.请参阅下面的示例,我使用了AWSSDK 3.1.6 .net(3.5):

you can use S3FileInfo class and Exists method of this class it will hep you to check if file exists without download the file .see the example below I used the AWSSDK 3.1.6 .net(3.5) :

public static  bool ExistsFile()
{
    BasicAWSCredentials basicCredentials = new BasicAWSCredentials("my access key", "my secretkey");
                AmazonS3Config configurationClient = new AmazonS3Config();
                configurationClient.RegionEndpoint = RegionEndpoint.EUCentral1;

                try
                {
                    using (AmazonS3Client clientConnection = new AmazonS3Client(basicCredentials, configurationClient))
                    {

                        S3FileInfo file = new S3FileInfo(clientConnection, "mybucket", "FolderNameUniTest680/FileNameUnitTest680");
                        return file.Exists;//if the file exists return true, in other case false
                    }
                }
                catch(Exception ex)
                {
                    return false;
                }
    }