XenForo 版本兼容性
下载仅与 XenForo 2.1 及更高版本兼容。
为什么选择本指南?
从 XenForo 2.0.0 开始,我们支持使用
名为 Flysystem 的抽象文件系统进行远程文件存储。它被称为抽象文件系统,因为它在代码和文件系统之间添加了一个抽象层。这意味着它为执行文件系统作提供了一致的 API,因此无论文件系统是基于本地磁盘的文件系统还是分布式和远程访问的文件系统,我们的代码都会调用相同的函数,而 Flysystem 会处理其余的工作。
尽管这很有用,但它并不是最明显或最直接的设置,因此本指南和随附的附加组件将有所帮助。
因此,如果您打算在 XF 2.1 或更高版本中使用
视频上传功能,并且您担心会增加磁盘空间要求,这将有所帮助。
提供
所需的文件尽管您可以自己下载文件并设置自动加载器之类的内容,但您可能更愿意简单地下载附加到此资源的附加组件。您可以
按通常的方式安装该附加组件。
开始
之前 如果您在现有站点上进行设置,则需要手动移动现有文件。最后有一节是关于这方面的。在移动现有文件、进行设置和测试时,我们建议您先关闭论坛。
设置 DigitalOcean Spaces
:我们将首先介绍这一点,因为它是最简单的设置。如果您更喜欢使用 Amazon S3,请跳到下面的
设置 Amazon S3 部分。
- 转到 DigitalOcean Cloud 页面并注册或登录。
- 此时,如果您是 DigitalOcean 的新用户,则可能需要设置计费。
- 您现在可以创建一个新项目。
- 单击“开始使用 Spaces”链接。
- 选择您的数据中心区域(我选择了 Amsterdam)。
- 保持 “Restrict File Listing” 处于选中状态。
- 选择一个唯一的名称(我选择了 “xftest”)
- 点击 [Create a space]
现在空间已创建,您应该有一个终端节点 URL,类似于:
xftest.ams3.digitaloceanspaces.com。记下来以备后用。
现在我们需要创建一些 API 凭证。作步骤:
- 点击左侧边栏中的 “Manage”(管理)。
- 点击 [API]。
- 在“Spaces access keys”部分,单击“Generate New Key”。
- 键入密钥的名称(同样,我选择了 “xftest”) 并保存。
这将为您提供一个密钥和一个密钥。记下它们。
配置 XF 以使用 DigitalOcean Spaces我们现在
需要配置 XF 以使用 DigitalOcean Spaces 进行文件存储。我们将从通常首先进入 data 目录的内容开始。这通常包括附件缩略图和头像。
打开您的文件。
首先,我们需要配置 Amazon S3 客户端(DigitalOcean Spaces API 与 Amazon AWS 开发工具包兼容)。
We will do this using a closure so that we can reuse the same code and we only have to type it out once:
PHP:
$s3 = function()
{
return new \Aws\S3\S3Client([
'credentials' => [
'key' => 'ABC',
'secret' => '123'
],
'region' => 'ams3',
'version' => 'latest',
'endpoint' => 'https://ams3.digitaloceanspaces.com'
]);
};
Note that the key and secret are what you noted down after setting up the "Spaces access key" earlier. The region can be inferred from the endpoint URL you noted down earlier. It's the part after the first . in the URL, in my case it is ams3. The endpoint is the same endpoint URL minus the unique name you chose.
Next we need to set up the actual Flysystem adapter to use the S3 client:
PHP:
$config['fsAdapters']['data'] = function() use($s3)
{
return new \League\Flysystem\AwsS3v3\AwsS3Adapter($s3(), 'xftest', 'data');
};
Finally, we need to ensure that avatar and attachment thumbnail URLs are prepended with the correct URL. This requires the endpoint URL you noted down earlier, again:
PHP:
$config['externalDataUrl'] = function($externalPath, $canonical)
{
return 'https://xftest.ams3.digitaloceanspaces.com/data/' . $externalPath;
};
At this point, everything should be working in terms of new uploads. Don't be alarmed if you notice that avatars and thumbnails are missing; if you have existing files, they will need to be moved over manually which we'll go through later.
First, we need to test that the configuration works. Simply go and upload a new avatar. The avatar will now be stored and served remotely!
If you check your DigitalOcean Spaces account now, you should see that new folders have been created containing your new avatar:
Success! 🌟 But we're only half way there!
We now need to add support for the internal_data directory stuff too. Generally, this is attachments and any other stuff that should be "private". Back to config.php and the code to add is very similar:
PHP:
$config['fsAdapters']['internal-data'] = function() use($s3)
{
return new \League\Flysystem\AwsS3v3\AwsS3Adapter($s3(), 'xftest', 'internal_data');
};
Now try to upload an attachment to a post and, much like before, you should now see additional files and folders in your Spaces file browser. 🎉
Setting up Amazon S3
- Go to the AWS Management Console page and sign up or log in.
- In the "AWS services" section type "S3" to go to the "S3 Console".
- Click "Create bucket".
- Choose a bucket name (I have chosen xftest).
- Choose a region (I have chosen EU London).
- Accept any further default options until the bucket is created.
- You now need to go to the "IAM" console.
- Click "Add user".
- Pick a username (yep, I used xftest again 😉).
- Set the access type to "Programmatic".
- To set permissions, click the "Attach existing policies directly" tab followed by the "Create policy" button.
- IAM and the various policies and permissions can be fairly daunting. We can make it a bit easier, though you may have different requirements. On this page there is a tab called "JSON". Paste the following in there, replacing YOUR-BUCKET-NAME with the bucket name you chose earlier:
JSON:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:putObject",
"s3:putObjectAcl",
"s3:ReplicateObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR-BUCKET-NAME",
"arn:aws:s3:::YOUR-BUCKET-NAME/*"
]
}
]
}
- Click "Review policy" give it a name and save.
- Go back to the previous "Add user" page, click the "Refresh" button and search for the policy you just created.
- Click "Next", followed by "Create user".
This will give you a key and a secret. Note them down.
Configuring XF to use Amazon S3
We now need to configure XF to use Amazon S3 for file storage. We'll start with what usually goes into the data directory first. This generally includes attachment thumbnails and avatars.
Open your src/config.php file.
We will do this using a closure so that we can reuse the same code and we only have to type it out once:
PHP:
$s3 = function()
{
return new \Aws\S3\S3Client([
'credentials' => [
'key' => 'ABC',
'secret' => '123'
],
'region' => 'eu-west-2',
'version' => 'latest',
'endpoint' => 'https://s3.eu-west-2.amazonaws.com'
]);
};
Note that the key and secret are what you noted down after setting up the IAM user earlier. The region can be inferred from the S3 endpoint URL.
Next we need to set up the actual Flysystem adapter to use the S3 client:
PHP:
$config['fsAdapters']['data'] = function() use($s3)
{
return new \League\Flysystem\AwsS3v3\AwsS3Adapter($s3(), 'xftest', 'data');
};
Finally, we need to ensure that avatar and attachment thumbnail URLs are prepended with the correct URL:
PHP:
$config['externalDataUrl'] = function($externalPath, $canonical)
{
return 'https://xftest.s3.eu-west-2.amazonaws.com/data/' . $externalPath;
};
At this point, everything should be working in terms of new uploads. Don't be alarmed if you notice that avatars and thumbnails are missing; if you have existing files, they will need to be moved over manually which we'll go through later.
First, we need to test that the configuration works. Simply go and upload a new avatar. The avatar will now be stored and served remotely!
If you check your bucket file browser now, you should see that new folders have been created containing your new avatar:
Success! 🌟 But we're only half way there!
We now need to add support for the internal_data directory stuff too. Generally, this is attachments and any other stuff that should be "private". Back to config.php and the code to add is very similar:
PHP:
$config['fsAdapters']['internal-data'] = function() use($s3)
{
return new \League\Flysystem\AwsS3v3\AwsS3Adapter($s3(), 'xftest', 'internal_data');
};
Now try to upload an attachment to a post and, much like before, you should now see additional files and folders in your bucket file browser. 🎉