简体   繁体   中英

Amazon S3 use virtual host for signed url

I am currently creating a system using AWS S3, my client needs a signed URL for his system. however I am currently having troubles setting up the virtual host for the signed URL.

Before the following code runs $source=http://media.example.com/object-key and $host=media.example.com

    $host = get_option('dn-s3-host');
        if (strpos($host,'http://') === FALSE) $host = 'http://' . $host;
        if (strpos($source,get_option('dn-s3-host')) !== FALSE) {
            $s3Client = new Aws\S3\S3Client(array(
                'version' => 'latest',
                'region'  => get_option('dn-s3-region'),
                'endpoint' => $host,
                'use_path_style_endpoint' => true,
                'force_path_style' => true,
                'credentials' => array(
                    // 'region'  => get_option('dn-s3-region'),
                    'key'=>get_option('dn-s3-key'),
                    'secret'=>get_option('dn-s3-secret')
                    )
            ));
            $source = str_replace('http://','',$source);
            $source = ltrim(str_replace(get_option('dn-s3-host'),'',$source),'/');
            $cmd = $s3Client->getCommand('GetObject',array(
                    'Bucket'=>get_option('dn-s3-bucket'),
                    'Key'=>$source
                ));
            $request = $s3Client->createPresignedRequest($cmd,'+2 hours');
            $source = (string) $request->getUri();
        }
    }

When this code runs, I am receiving this error:

<Error>
    <Code>NoSuchKey</Code>
    <Message>The specified key does not exist.</Message>
    <Key>bucket/object-key</Key>
    <RequestId>363C44A299AA1E88</RequestId>
    <HostId>
     aCUab3+AD6396dJRMX8rCfYDl9EMjEBDn9somLQa3Ii6Bm6GoJGHT+f/qSZGprcR+3iG/hXC3EQ=
    </HostId>
</Error>

My virtual host media.example.com is a CNAME record pointing to bucket.s3.amazonaws.com

If I access http://media.example.com/object-key I get the file correctly. How do I set this virtual host for the signed URL?

If your Signed URL is invalid you will get error like below,

<Error>
<Code>MissingKey</Code>
<Message>
Missing Key-Pair-Id query parameter or cookie value
</Message>
</Error>

The error indicates, that the object does not exist.

Based on the error message,

 <Key>bucket/object-key</Key>

Looks like you have bucket repeated twice in your URL.

Try to print the actual signed URL and check what it is the URL looks like, that will give an idea of what error you made there.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM