firebrandhq/s3fileupload

扩展 SilverStripe UploadField,允许将文件存储在 Amazon S3 中

安装: 489

依赖项: 0

建议者: 0

安全: 0

星星: 2

观察者: 4

分支: 3

开放问题: 0

类型:silverstripe-module

v1.0.0 2015-07-16 11:17 UTC

This package is auto-updated.

Last update: 2024-09-14 10:01:11 UTC


README

此 SilverStripe 插件允许文件直接上传到 S3 并作为 SilverStripe DataObjects 跟踪。

要求

  • SilverStripe 3.1
  • AWS SDK for PHP v3

安装

通过 composer 安装模块

composer require firebrandhq/s3fileupload

设置您的 S3 存储桶

要能够直接上传到 S3,您的存储桶必须具有 CORS 配置。要设置 CORS 配置

  1. 登录到 S3 并访问您的存储桶。
  2. 选择 属性 以查看存储桶属性。
  3. 权限 下,选择 编辑 CORS 配置
  4. 在弹出窗口中,以 XML 格式输入您的 CORS 配置。

示例 CORS 配置

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://example.com</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>Location</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
    <CORSRule>
        <AllowedOrigin>https://dev.example.com</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>Location</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
    <CORSRule>
        <AllowedOrigin>http://localhost</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>Location</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

请注意,您需要为计划从其中上传文件的每个域创建一个单独的 CORSRule 条目。

您还需要为每个协议创建一个 CORSRule 条目。

获取访问密钥和密钥

您需要生成 AWS Access Key ID 和 Secret Access Key 以允许 SilverStripe 与 S3 通信。

出于安全原因,强烈建议 创建一个新的具有有限访问权限的用户。这样,如果您的访问密钥和/或密钥遭到泄露,可以最小化损失。

您的 AWS 用户的示例策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:ListAllMyBuckets",
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::name-of-your-bucket",
                "arn:aws:s3:::name-of-your-bucket/*"
            ]
        }
    ]
}

配置 SilverStripe

您可以使用 YML 配置文件来告诉 SilverStripe 如何访问您的存储桶。

mysite/_config 中创建一个 s3.yml 文件。

S3File:
  Bucket: 'name-of-your-bucket'
  Region: 'us-east-1'
  AccessId: 'YOUACCESSKEYID'
  Secret: 'YOURACCESSKEYSECRET'

如果不需要,您无需在 YML 文件中提供存储桶或区域。这些可以在 S3FileUploadField 中手动设置。

将 S3File 添加到 SilverStripe DataObject/Page

<?php
class Page extends SiteTree {

	private static $has_one = array(
        'File' => 'S3File'
	);

    public function getCMSFields() {
		$fields = parent::getCMSFields();

		$s3Field = S3FileUploadField::create('File', 'S3 File')
            ->setAllowedMaxFileNumber(1);

        // You can omit the following 2 lines.
        // It will fallback on the YML configuration.
        $s3Field->setBucket('YourBucketName');
        $s3Field->setRegion('us-east-1');

		$fields->insertBefore(
			S3FileUploadField::create('S3File', 'S3 File')
				->setAllowedMaxFileNumber(1),
			'Description'
		);

		$fields->addFieldToTab('Root.Main',$s3Field);

		return $fields;
	}

}

在 SS 模板中引用文件

<h2>Simple link</h2>
<p>
$File <br/>
You can customize this by overriding S3File.ss
</p>

<h2>Full file details</h2>
<ul>
    <li><strong>File name:</strong> $File.Name </li>
    <li><strong>Size:</strong> $File.SizeForHuman </li>
    <li><strong>Type:</strong> $File.Extension.UpperCase </li>
    <li><strong>Last modified:</strong> $File.LastModified.Nice </li>
</ul>

<a href="{$File.TemporaryDownloadLink}">Download</a>