如何获取我刚刚放入Amazon S3存储桶的对象的URL?

时间:2016-02-10 10:52:23

标签: java amazon-s3 aws-sdk

使用Amazon S3 Java SDK,我可以使用

将对象放入S3存储桶
PutObjectResult
但是,这会返回一个CompleteMultipartUpload,它似乎不包含一个字段,该字段包含可以访问上载资源的URL。我希望有类似location' URL url = s3.getUrl(bucketName, key); 字段的内容。

我知道我可以用

跟进上述请求
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Views"
    android:paddingLeft="10dp"
    android:textSize="20sp"
    />

<RadioGroup
    android:id="@+id/radioView"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:measureWithLargestChild="true"
    android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioViewSingle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/single"
            android:layout_weight="1"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioView2by2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view2x2" />

        <RadioButton
            android:id="@+id/radioView3by3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view3x3" />

        <RadioButton
            android:id="@+id/radioView4by4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableRight="@drawable/view4x4" />
</RadioGroup>
</LinearLayout>

但我真的很想避免发出两个请求来完成此任务。

1 个答案:

答案 0 :(得分:1)

试试这个。

using (var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
    GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
    {
        BucketName = "MyBucket",
        Key = "MyKey",
        Expires = DateTime.Now.AddMinutes(5)
    };
    string urlString = s3Client.GetPreSignedURL(request1);
}

谢谢!