如何使用 "temporaryUrl" 和 Laravel::Storage 中的自定义 cname with Amazon S3 -> CloudFront 并使用 Cname

How to use "temporaryUrl" with custom cname in Laravel::Storage with Amazon S3 -> CloudFront and using a Cname

我在 S3 中有一个文件,CloudFront 使用 cname(带有亚马逊 SSL 证书),而文件是 public 我可以使用 URL.[=19 毫无问题地访问它=]

public 个文件中的有效示例:

https://xxxxxxxxxxxxx.cloudfront.net/media/logos/logo1.png
https://cdn.{mydomain.com}/media/logos/logo1.png
https://s3.amazonaws.com/{mys3bucketname}/media/logos/logo1.png

在laravel

$disk = Storage::disk('cnames3');
$tempUrl = $disk->temporaryUrl($file, now()->addMinutes(5));

我找到的最佳选择是: Should I use CloudFront together as TemporaryUrl for sensitive files in s3

'cnames3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
]

=====
.env
AWS_BUCKET={mys3bucketname}
AWS_ENDPOINT=https://xxxxxxxxxxxxx.cloudfront.net
AWS_URL=https://cdn.{mydomain.com}

但是我生成的 URL 包含存储桶的名称,因此它对我不起作用,因为它拒绝我访问。

https://cdn.{mydomain.com}/{mys3bucketname}/media/logos/logoprivate.png?{params}

我怎样才能获得与 CNAME 兼容的 URL,或者我该怎么做才能使用我自己的带签名 URL 的域;我寻找这种格式:

https://cdn.{mydomain.com}/media/logos/logoprivate.png?{params}

如果我有私有文件并使用 "temporaryUrl" 没有端点 它 returns 一个有效的 url:

 https://s3.amazonaws.com/{mys3bucketname}/media/logos/logoprivate.png?{params}

但是没有我的域,这对我不起作用,我一直在寻找解决方案几个小时,我希望你能帮助这个主题的初学者

您必须在配置中将 bucket_endpoint 设置为 true,这样它就不会在您的域中附加 bucket name

'cnames3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'bucket_endpoint' => true,  //add this 
        'endpoint' => env('AWS_ENDPOINT'),
]

你可以在这里查看https://github.com/aws/aws-sdk-php/blob/master/src/S3/S3Client.php

(假设带有 CNAME 的 CloudFront 已经在工作)

in options of CloudFrontDistributions>{yourCFID} In Origins tab edit:

Origin Access Identity -> Use an Identity

Restrict Bucket Access: yes (and Yes, Update Bucket Policy)

In Default Cache Behavior Settings:

Restrict Viewer Access (Use Signed URLs or Signed Cookies) = YES

Trusted Signers = SELF

1.- 首先从 CloudFront 创建私钥

Creating CloudFront Key Pairs for Your Trusted Signers

2.-

composer require league/flysystem-aws-s3-v3

composer require aws/aws-sdk-php

3.- 创建函数: Signing CloudFront URLs for Private Distributions

示例:

 use Aws\CloudFront\CloudFrontClient;

...

//$filesystemDisk = "s3"
private function signUrl($filesystemDisk, $resourceKey = null)
{
    $cloudFront = new CloudFrontClient([
        'region'  => config('filesystems.disks.' . $filesystemDisk . '.region'),
        'version' => '2014-11-06',
    ]);
    // Set up parameter values for the resource
    //example
    $resourceKey = 'https://cdn.mydomain.com/media/logos/logoprivate.jpg';
    $expires = time() + 200;

// Create a signed URL for the resource using the canned policy
        $signedUrlCannedPolicy = $cloudFront->getSignedUrl([
            'url'         => $resourceKey,
            'expires'     => $expires,
            'private_key' => '/path/to/keys/amazon/cloudfront/private/pk-APKFYWFAKEFAKEFAKEIQ.pem',
            'key_pair_id' => 'APKFYWFAKEFAKEFAKEIQ',
        ]);

        return $signedUrlCannedPolicy;
    }

4.- 完成;你得到(用你的cname):

https://cdn.{mydomain.com}/media/logos/logoprivate.jpg?{params}