<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java S3 示例 > 原文: [https://javatutorial.net/java-s3-example](https://javatutorial.net/java-s3-example) 在本教程中,我將解釋如何通過 Amazon 提供的 Java API 使用 Amazon 的 S3 存儲。 該示例說明了如何創建存儲分區,列出存儲分區的內容,在存儲分區中創建文件夾,上傳文件,為文件提供公共訪問權限以及如何刪除所有這些項目。 ## ![amazon s3 java example](https://img.kancloud.cn/74/79/747971b9303606d6ca8ec41d8b30fdab_592x375.jpg) ## 設置項目 1. 您將需要適用于 Java 的 AWS 開發工具包,此示例才能正常工作。 如果您現在尚未下載 SDK,[請在此處下載](http://aws.amazon.com/sdk-for-java/)。 您還需要將存檔中的`.JAR`文件集成到您的項目中。 或者,您可以使用具有以下依賴項的 Maven ```java <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.9.2</version> </dependency> ``` 2. 如果您沒有,請在 Amazon IAM(https://console.aws.amazon.com/iam)中創建一個用戶。 在這里您將獲得一個“訪問密鑰”和“秘密訪問密鑰”。 您將需要此憑據才能連接到 S3。 3. 將 IAM 中的“AWSConnector”和“ AmazonS3FullAccess”權限添加到新用戶。 沒有這個,您希望能夠通過服務器進行身份驗證。 ## 使用 Amazon S3 進行身份驗證 有 4 種不同的方法可針對 Amazon S3 驗證您的請求 **1\. 使用默認的憑證配置文件** – 這是 Amazon 推薦的方法。 創建具有以下結構的文件并填寫訪問密鑰: ```java # Move this credentials file to (~/.aws/credentials) # after you fill in your access and secret keys in the default profile # WARNING: To avoid accidental leakage of your credentials, # DO NOT keep this file in your source directory. [default] aws_access_key_id= aws_secret_access_key= ``` 默認情況下,將此文件保存為`.aws`文件夾中文件名`credentials`下的 Windows 用戶或 Linux 中主目錄的 `C:\Users\user\.aws\credentials` 如果使用此方法,則可以在代碼中創建一個`Credentials`對象,如下所示: ```java AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials(); ``` **2\. 使用環境變量** – 設置系統中以下環境變量的值。`AWS_ACCESS_KEY_ID`和`AWS_SECRET_ACCESS_KEY` **3\. Java 系統屬性** – `aws.accessKeyId`和`aws.secretKey`。 使用`SystemPropertiesCredentialsProvider`在程序中加載變量 **4\. 以編程方式設置憑據** – 在此示例中,我將使用此方法,因為它更易于遵循 在代碼中使用以下代碼: ```java AWSCredentials credentials = new BasicAWSCredentials("YourAccessKeyID", "YourSecretAccessKey"); ``` ## 創建 S3 客戶端 為了能夠與 S3 通信,您必須使用`AmazonS3`的實現。 您將使用實例來解決對服務器的請求 ```java AmazonS3 s3client = new AmazonS3Client(credentials); ``` ## 創建桶 存儲桶在整個 S3 領域中必須具有唯一的名稱 ```java String bucketName = "javatutorial-net-example-bucket"; s3client.createBucket(bucketName); ``` ## 列出桶 您可以像這樣從所有桶中獲得列出 ```java for (Bucket bucket : s3client.listBuckets()) { System.out.println(" - " + bucket.getName()); } ``` ## 在 S3 存儲桶中創建文件夾 使用此代碼在存儲桶中創建一個空文件夾 ```java public static void createFolder(String bucketName, String folderName, AmazonS3 client) { // create meta-data for your folder and set content-length to 0 ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); // create empty content InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // create a PutObjectRequest passing the folder name suffixed by / PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + SUFFIX, emptyContent, metadata); // send request to S3 to create folder client.putObject(putObjectRequest); } ``` ## 在 S3 中上傳文件 如果要將文件上傳到文件夾,請使用此 ```java String fileName = folderName + SUFFIX + "testvideo.mp4"; s3client.putObject(new PutObjectRequest(bucketName, fileName, new File("C:\\Users\\user\\Desktop\\testvideo.mp4"))); ``` 只需刪除文件名中的文件夾和后綴即可直接上傳到存儲桶。 如果要公開文件(Amazon S3 默認情況下文件為私有文件),請將其設置為`PutObjectRequest`(更多信息請參見下面的完整示例) ```java .withCannedAcl(CannedAccessControlList.PublicRead) ``` ## 刪除文件,文件夾和存儲桶 要刪除存儲桶,請使用此功能。 儲存桶必須為空,否則您無法將其刪除 ```java s3client.deleteBucket(bucketName); ``` 要刪除文件,請使用: ```java s3client.deleteObject(bucketName, fileName); ``` 要刪除文件夾,您必須先刪除其中的所有文件。 請查看下面的完整示例以獲取更多信息。 ## 完整的例子 在這里,您可以在一個工作程序中找到以上所有片段。 出于可讀性的考慮,排除了異常處理,請不要忘記在代碼中添加異常處理 ```java import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.S3ObjectSummary; public class AmazonS3Example { private static final String SUFFIX = "/"; public static void main(String[] args) { // credentials object identifying user for authentication // user must have AWSConnector and AmazonS3FullAccess for // this example to work AWSCredentials credentials = new BasicAWSCredentials( "YourAccessKeyID", "YourSecretAccessKey"); // create a client connection based on credentials AmazonS3 s3client = new AmazonS3Client(credentials); // create bucket - name must be unique for all S3 users String bucketName = "javatutorial-net-example-bucket"; s3client.createBucket(bucketName); // list buckets for (Bucket bucket : s3client.listBuckets()) { System.out.println(" - " + bucket.getName()); } // create folder into bucket String folderName = "testfolder"; createFolder(bucketName, folderName, s3client); // upload file to folder and set it to public String fileName = folderName + SUFFIX + "testvideo.mp4"; s3client.putObject(new PutObjectRequest(bucketName, fileName, new File("C:\\Users\\user\\Desktop\\testvideo.mp4")) .withCannedAcl(CannedAccessControlList.PublicRead)); deleteFolder(bucketName, folderName, s3client); // deletes bucket s3client.deleteBucket(bucketName); } public static void createFolder(String bucketName, String folderName, AmazonS3 client) { // create meta-data for your folder and set content-length to 0 ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); // create empty content InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // create a PutObjectRequest passing the folder name suffixed by / PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + SUFFIX, emptyContent, metadata); // send request to S3 to create folder client.putObject(putObjectRequest); } /** * This method first deletes all the files in given folder and than the * folder itself */ public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) { List fileList = client.listObjects(bucketName, folderName).getObjectSummaries(); for (S3ObjectSummary file : fileList) { client.deleteObject(bucketName, file.getKey()); } client.deleteObject(bucketName, folderName); } } ``` 如果您認為本教程有幫助,請查看我們的其他教程 - 我們確定您會在此頁面上找到其他有趣的內容。 隨時在“評論”部分中提問,或者通過共享本教程向我們展示一些愛意??
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看