

 適用於 .NET 的 AWS SDK V3 已進入維護模式。

我們建議您遷移至 [適用於 .NET 的 AWS SDK V4](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/welcome.html)。如需如何遷移的其他詳細資訊和資訊，請參閱我們的[維護模式公告](https://aws.amazon.com/blogs/developer/aws-sdk-for-net-v3-maintenance-mode-announcement/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 的特殊考量 適用於 .NET 的 AWS SDK
<a name="special-considerations"></a>

本節包含一般組態或程序不適當或不足的特殊情況考量。

**Topics**
+ [取得 AWSSDK 組件](net-dg-obtain-assemblies.md)
+ [在應用程式中存取登入資料和設定檔](creds-locate.md)
+ [Unity 支援](unity-special.md)
+ [Xamarin 支援](xamarin-special.md)

# 取得 的組件 適用於 .NET 的 AWS SDK
<a name="net-dg-obtain-assemblies"></a>

本主題說明如何取得 AWSSDK 組件，並將其存放在本機 （或內部部署） 以供專案使用。**這不是**處理 SDK 參考的建議方法，但在某些環境中是必要的。

**注意**  
處理 SDK 參考的建議方法是僅下載並安裝每個專案所需的 NuGet 套件。該方法如 中所述[使用 NuGet 安裝 AWSSDK 套件](net-dg-install-assemblies.md)。

如果您無法或無法依專案下載和安裝 NuGet 套件，您可以使用下列選項。

## 下載並擷取 ZIP 檔案
<a name="download-zip-files"></a>

（請記住，這不是處理 參考[的建議方法](net-dg-install-assemblies.md) 適用於 .NET 的 AWS SDK。)

1. 下載下列其中一個 ZIP 檔案：
   + [aws-sdk-net8.0.zip](https://sdk-for-net.amazonwebservices.com/latest/v3/aws-sdk-net8.0.zip) - 支援 .NET 8 和更新版本的組件。
   + [aws-sdk-netcoreapp3.1.zip](https://sdk-for-net.amazonwebservices.com/latest/v3/aws-sdk-netcoreapp3.1.zip) - 支援 .NET Core 3.1 和更新版本的組件。
   + [aws-sdk-netstandard2.0.zip](https://sdk-for-net.amazonwebservices.com/latest/v3/aws-sdk-netstandard2.0.zip) - 支援 .NET Standard 2.0 和 2.1 的組件。
   + [aws-sdk-net45.zip](https://sdk-for-net.amazonwebservices.com/latest/v3/aws-sdk-net45.zip) - 支援 .NET Framework 4.5 和更新版本的組件。
   + [aws-sdk-net35.zip](https://sdk-for-net.amazonwebservices.com/latest/v3/aws-sdk-net35.zip) - 支援 .NET Framework 3.5 的組件。
**警告**  
從 2024 年 8 月 15 日開始， 適用於 .NET 的 AWS SDK 將結束對 .NET Framework 3.5 的支援，並將最低 .NET Framework 版本變更為 4.7.2。如需詳細資訊，請參閱部落格文章 [.NET Framework 3.5 和 4.5 目標的重要變更 適用於 .NET 的 AWS SDK](https://aws.amazon.com/blogs/developer/important-changes-coming-for-net-framework-3-5-and-4-5-targets-of-the-aws-sdk-for-net/)。

1. 將組件解壓縮到檔案系統上的一些「下載」資料夾；位置並不重要。請記下此資料夾。

1. 當您設定專案時，您可以從此資料夾取得必要的組件，如中所述[安裝不含 NuGet 的 AWSSDK 組件](net-dg-install-without-nuget.md)。

# 在應用程式中存取登入資料和設定檔
<a name="creds-locate"></a>

使用登入資料的偏好方法是允許 為您 適用於 .NET 的 AWS SDK 尋找和擷取登入資料，如 中所述[憑證和設定檔解析](creds-assign.md)。

不過，您也可以設定應用程式主動擷取設定檔和登入資料，然後在建立 AWS 服務用戶端時明確使用這些登入資料。

若要主動擷取設定檔和登入資料，請使用 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間中的類別。
+ 若要在使用 AWS 登入資料檔案格式的檔案中尋找設定檔 ([其預設位置中的共用 AWS 登入資料檔案](creds-file.md)或自訂登入資料檔案），請使用 [SharedCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TSharedCredentialsFile.html) 類別。為了簡潔起見，此格式的檔案有時會在此文字中簡單地稱為*登入資料檔案*。
+ 若要在 SDK Store 中尋找設定檔，請使用 [NetSDKCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TNetSDKCredentialsFile.html) 類別。
+ 若要在登入資料檔案和 SDK Store 中搜尋，根據類別屬性的組態，請使用 [CredentialProfileStoreChain](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TCredentialProfileStoreChain.html) 類別。

  您可以使用此類別來尋找設定檔。您也可以使用此類別直接請求 AWS 登入資料，而不是使用`AWSCredentialsFactory`類別 （接下來說明）。
+ 若要從設定檔擷取或建立各種類型的登入資料，請使用 [AWSCredentialsFactory](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TAWSCredentialsFactory.html) 類別。

下列各節提供這些類別的範例。

## 類別 CredentialProfileStoreChain 的範例
<a name="creds-locate-chain"></a>

您可以使用 [TryGetAWSCredentials](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/MCredentialProfileStoreChainTryGetAWSCredentialsStringAWSCredentials.html) 或 [TryGetProfile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/MCredentialProfileStoreChainTryGetProfileStringCredentialProfile.html) 方法，從 [CredentialProfileStoreChain](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TCredentialProfileStoreChain.html) 類別取得登入資料或設定檔。類別的 `ProfilesLocation` 屬性決定方法的行為，如下所示：
+ 如果 `ProfilesLocation` 是 null 或空白，請在平台支援時搜尋 SDK Store，然後在預設位置搜尋共用的 AWS 登入資料檔案。
+ 如果 `ProfilesLocation` 屬性包含值，請搜尋 屬性中指定的登入資料檔案。

### 從 SDK Store 或共用的登入資料檔案取得 AWS 登入資料
<a name="creds-locate-chain-get-credentials-default-location"></a>

此範例說明如何使用 `CredentialProfileStoreChain`類別取得登入資料，然後使用登入資料來建立 [AmazonS3Client](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Client.html) 物件。登入資料可以來自軟體開發套件存放區，也可以來自預設位置的共用 AWS 登入資料檔案。

此範例也使用 [Amazon.Runtime.AWSCredentials](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TAWSCredentials.html) 類別。

```
var chain = new CredentialProfileStoreChain();
AWSCredentials awsCredentials;
if (chain.TryGetAWSCredentials("some_profile", out awsCredentials))
{
    // Use awsCredentials to create an Amazon S3 service client
    using (var client = new AmazonS3Client(awsCredentials))
    {
        var response = await client.ListBucketsAsync();
        Console.WriteLine($"Number of buckets: {response.Buckets.Count}");
    }
}
```

### 從 SDK Store 或共用 AWS 登入資料檔案取得設定檔
<a name="creds-locate-chain-get-profile-default-location"></a>

此範例說明如何使用 CredentialProfileStoreChain 類別取得設定檔。登入資料可以來自軟體開發套件存放區，也可以來自預設位置的共用 AWS 登入資料檔案。

此範例也會使用 [CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TCredentialProfile.html) 類別。

```
var chain = new CredentialProfileStoreChain();
CredentialProfile basicProfile;
if (chain.TryGetProfile("basic_profile", out basicProfile))
{
    // Use basicProfile
}
```

### 從自訂登入資料檔案取得登入資料
<a name="creds-locate-chain-get-credentials-alternate-location"></a>

此範例說明如何使用 CredentialProfileStoreChain 類別取得登入資料。登入資料來自使用 AWS 登入資料檔案格式的檔案，但位於替代位置。

此範例也使用 [Amazon.Runtime.AWSCredentials](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TAWSCredentials.html) 類別。

```
var chain = new
    CredentialProfileStoreChain("c:\\Users\\sdkuser\\customCredentialsFile.ini");
AWSCredentials awsCredentials;
if (chain.TryGetAWSCredentials("basic_profile", out awsCredentials))
{
    // Use awsCredentials to create an AWS service client
}
```

## SharedCredentialsFile 和 AWSCredentialsFactory 類別的範例
<a name="creds-locate-cred-shared-file"></a>

### 使用 SharedCredentialsFile 類別建立 AmazonS3Client
<a name="creds-locate-cred-shared-file-create-s3-client"></a>

此範例說明如何在共用 AWS 登入資料檔案中尋找設定檔、從設定檔建立 AWS 登入資料，然後使用登入資料來建立 [AmazonS3Client](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Client.html) 物件。此範例使用 [SharedCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TSharedCredentialsFile.html) 類別。

此範例也會使用 [CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TCredentialProfile.html) 類別和 [Amazon.Runtime.AWSCredentials](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TAWSCredentials.html) 類別。

```
CredentialProfile basicProfile;
AWSCredentials awsCredentials;
var sharedFile = new SharedCredentialsFile();
if (sharedFile.TryGetProfile("basic_profile", out basicProfile) &&
    AWSCredentialsFactory.TryGetAWSCredentials(basicProfile, sharedFile, out awsCredentials))
{
    // use awsCredentials to create an Amazon S3 service client
    using (var client = new AmazonS3Client(awsCredentials, basicProfile.Region))
    {
        var response = await client.ListBucketsAsync();
        Console.WriteLine($"Number of buckets: {response.Buckets.Count}");
    }
}
```

**注意**  
[NetSDKCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Runtime/TNetSDKCredentialsFile.html) 類別的使用方式完全相同，但您會執行個體化新的 NetSDKCredentialsFile 物件，而不是 SharedCredentialsFile 物件。

# Unity 支援的特殊考量
<a name="unity-special"></a>

為您的 Unity 應用程式使用 適用於 .NET 的 AWS SDK 和 [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) 時，您的應用程式必須直接參考 適用於 .NET 的 AWS SDK 組件 (DLL 檔案），而不是使用 NuGet。有鑑於此需求，以下是您需要執行的重要動作。


+ 您需要取得 適用於 .NET 的 AWS SDK 組件並將其套用至您的專案。如需如何執行此操作的詳細資訊，請參閱主題 [下載並擷取 ZIP 檔案](net-dg-obtain-assemblies.md#download-zip-files)中的 [取得 AWSSDK 組件](net-dg-obtain-assemblies.md)。
+ 您需要在 Unity 專案中包含下列 DLLs，以及 **AWSSDK.Core** 的 DLLs 和您正在使用的其他 AWS 服務。從 3.5.109 版開始 適用於 .NET 的 AWS SDK，.NET Standard ZIP 檔案包含這些額外的 DLLs。
  + [Microsoft.Bcl.AsyncInterfaces.dll](https://www.nuget.org/packages/Microsoft.Bcl.AsyncInterfaces/)
  + [System.Runtime.CompilerServices.Unsafe.dll](https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/)
  + [System.Threading.Tasks.Extensions.dll](https://www.nuget.org/packages/System.Threading.Tasks.Extensions/)
+ 如果您使用 [IL2CPP](https://docs.unity3d.com/Manual/IL2CPP.html) 建置 Unity 專案，則必須將`link.xml`檔案新增至資產資料夾，以防止程式碼分割。`link.xml` 檔案必須列出您使用的所有 AWSSDK 組件，且每個組件都必須包含 `preserve="all"` 屬性。下列程式碼片段顯示此檔案的範例。

  ```
  <linker>
      <assembly fullname="AWSSDK.Core" preserve="all"/>
      <assembly fullname="AWSSDK.DynamoDBv2" preserve="all"/>
      <assembly fullname="AWSSDK.Lambda" preserve="all"/>
  </linker>
  ```

**注意**  
若要閱讀與此需求相關的有趣背景資訊，請參閱文章：https：//[https://aws.amazon.com/blogs/developer/referencing-the-aws-sdk-for-net-standard-2-0-from-unity-xamarin-or-uwp/](https://aws.amazon.com/blogs/developer/referencing-the-aws-sdk-for-net-standard-2-0-from-unity-xamarin-or-uwp/)。

除了這些特殊考量之外，[3.5 版的變更](net-dg-v35.md#net-dg-v35-changes)如需將 Unity 應用程式遷移至 3.5 版的資訊，請參閱 適用於 .NET 的 AWS SDK。

# Xamarin 支援的特殊考量
<a name="xamarin-special"></a>

Xamarin 專案 (全新和現有) 必須將目標鎖定於 .NET Standard 2.0。請參閱[Xamarin.Forms 中的 .NET Standard 2.0 支援](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/net-standard)和 [.NET 實作支援](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support)。

另請參閱[可攜式類別程式庫和 Xamarin](net-dg-supported-platforms.md#portable-class-library) 的相關資訊。