gpt4 book ai didi

c# - 方法未找到 : void Amazon. S3.AmazonS3Client..ctor() |带有 .NET/Xamarin 的 AmazonS3

转载 作者:行者123 更新时间:2023-12-03 14:47:31 26 4
gpt4 key购买 nike

我目前正在关注 Amazon 的 AWSSDK 文档 (in particular: this),以尝试访问 Xamarin 表单应用程序上的 AmazonS3 数据。我遇到了一个错误,所以我用新的包开始了一个新项目,据我所知都是最新的,但仍然遇到同样的问题。

这是类背后的主要代码的当前代码:

public partial class MainPage : ContentPage
{
private static IAmazonS3 client;
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;


public MainPage()
{
InitializeComponent();

// I have tried each of the following:

client = new AmazonS3Client(); <--- ERROR
client = new AmazonS3Client(RegionEndpoint.USWest2); <-- SIMILAR ERROR
client = new AmazonS3Client(bucketRegion); <-- SAME ERROR

// This has a different error, lack of RegionEndpoint, which is expected, but at least this seems to be recognized
client = new AmazonS3Client(new AnonymousAWSCredentials());

}
}

这导致以下 运行时间 指示行上的错误:

Method not found: void Amazon.S3.AmazonS3Client..ctor()



我知道 MethodNotFoundExceptions 通常是由于旧版本的 DLL、过时的依赖项、重复的程序集等造成的,但我已经从一个全新的项目中部署了它,安装的唯一包是最新的标准 Xamarin 包和Amazon.S3 包。

是什么导致了这个错误?

最佳答案

该问题与“旧的 dll、过时的依赖项”等无关。如果您查看所遵循的示例,他们不会尝试调用默认 ctor,而是通过存储区区域对其进行初始化。

如果你看看Simple cross-platform application using the AWS SDK for .NET (甚至在您正在使用的 IDE 内的文档中),您可以看到在使用默认 ctor 时,您需要:

Before running this app, credentials must be specified either in the [default] profile or in another profile and then by setting the AWS_PROFILE environment variable. A region must be specified either in the [default] profile or by setting the AWS_REGION environment variable.



文档还说可以使用 App.config。

但是由于我们将它用于 Xamarin,因此使用其他一些重载会容易得多。这里是其中的一些:

enter image description here

假设您想使用 AWSCredentials为了生成您的客户。同样,您在这里有很多选择:
enter image description here

注意:当您初始化客户端时,最好指定 RegionEndpoint .大多数情况下,如果你忘记了,你会得到一个 AmazonClientException: No RegionEndpoint or ServiceURL configured所以它会提醒你它是必需的。

编辑:由于您的问题有更新,这里是我的更新:
相同的规则适用于您尝试过的那些 ctors。他们在配置文件中寻找凭据,而这些凭据在 Xamarin.Forms 应用程序中不存在。为了使用客户端,它需要知道它的凭据。如果您需要使用客户端,请在初始化期间为其提供一些凭证 - 使用其他一些 AWSCredentials - Basic , Federated等,或使用简单的 - 与 accessKeyId + accessKey .

如果您想知道为什么您尝试过的 ctors 不起作用,或者他们在幕后做了什么,他们的 SDK 是开源的 here .空ctor的代码是 here还有更有趣的 FallbackCredentialsFactory here .

关于c# - 方法未找到 : void Amazon. S3.AmazonS3Client..ctor() |带有 .NET/Xamarin 的 AmazonS3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62054022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com