【发布时间】:2022-07-08 12:08:33
【问题描述】:
有人可以帮助我如何将现有文件上传和链接到 s3。 WP Offload Media Lite 提供此功能,但需要付费。有没有其他方法可以做到这一点?
【问题讨论】:
标签: wordpress amazon-s3 upload
有人可以帮助我如何将现有文件上传和链接到 s3。 WP Offload Media Lite 提供此功能,但需要付费。有没有其他方法可以做到这一点?
【问题讨论】:
标签: wordpress amazon-s3 upload
解决方案:我只需一个简单的 SQL 查询就可以做到这一点,并将 wp-content/uploads 文件夹直接移动到 s3。更新链接不需要替换。 用于链接的 WP Offload Media Lite 插件表 'wp_as3cf_items'
将现有文件链接到 s3 的查询:
INSERT IGNORE INTO wp_as3cf_items (provider,region,bucket,path,original_path,is_private,source_type,source_id,source_path,original_source_path,extra_info,originator,is_verified) SELECT 'aws', 'us-_REGION_HERE', 'BUCKET_NAME_HERE', concat('wp-content/uploads/',SUBSTRING_INDEX(guid, 'wp-content/uploads/', -1) ) AS path, concat('wp-content/uploads/',SUBSTRING_INDEX(guid, 'wp-content/uploads/', -1)) AS original_path, 0, 'media-library', id as source_id, SUBSTRING_INDEX(guid, 'wp-content/uploads/', -1) AS source_path, SUBSTRING_INDEX(guid, 'wp-content/uploads/', -1) AS original_source_path, 'a:2:{s:13:"private_sizes";a:0:{}s:14:"private_prefix";s:0:"";}', 0, 1 FROM `wp_posts` WHERE `post_type` = 'attachment';
对于多站点,为每个带有表前缀的站点执行此操作。
【讨论】:
由于您使用的是免费版本,因此您必须手动上传所有现有文件到 AWS S3,然后您需要运行以下功能 更新 wp_as3cf_items 表
function wh_syncWPMediaLiteTable() {
global $wpdb;
$wpUploadDir = rtrim(wp_get_upload_dir()[‘basedir’], ‘/’);
$itemsTable = ‘as3cf_items’;
$offloadSettings = get_option(‘tantan_wordpress_s3’);
$lastAttachmentID = get_option(‘_site_transient_wh_last_sync_attachment’, 0);
//print_r($offloadSettings);
$objectPrefix = rtrim($offloadSettings[‘object-prefix’], ‘/’);
$format = [
‘provider’ => ‘%s’,
‘region’ => ‘%s’,
‘bucket’ => ‘%s’,
‘path’ => ‘%s’,
‘original_path’ => ‘%s’,
‘is_private’ => ‘%d’,
‘source_type’ => ‘%s’,
‘source_id’ => ‘%d’,
‘source_path’ => ‘%s’,
‘original_source_path’ => ‘%s’,
‘extra_info’ => ‘%s’,
‘originator’ => ‘%d’,
‘is_verified’ => ‘%d’
];
ksort($format);
$format = array_values($format);
#Getting the list of attachments
$sql = $wpdb->prepare("SELECT p.`ID`,
MAX(CASE WHEN pm.meta_key = ‘_wp_attached_file’ THEN pm.meta_value END ) as ‘attached_file’,
MAX(CASE WHEN pm.meta_key = ‘_wp_attachment_metadata’ THEN pm.meta_value END ) as ‘attachment_metadata’,
ai.`source_id`
FROM `{$wpdb->prefix}posts` AS p
LEFT JOIN `{$wpdb->prefix}postmeta` AS pm ON p.`ID` = pm.`post_id`
LEFT JOIN `{$wpdb->prefix}{$itemsTable}` AS ai ON p.`ID` = ai.`source_id`
AND ai.`source_id` IS NULL
WHERE p.`post_type` = %s
AND p.`ID` > %d
GROUP BY p.`ID`
LIMIT %d", ‘attachment’, $lastAttachmentID, 10);
$attachments = $wpdb->get_results($sql);
if (empty($attachments)) {
#trigger a mail to site admin that no attachments are left
die(‘All the images are synced into WP Offload Media Lite table.’);
}
foreach ($attachments as $key => $attachment) {
$lastAttachmentID = $attachment->ID;
$attachmentMetadata = maybe_unserialize($attachment->attachment_metadata);
$fullFilePath = $wpUploadDir . ‘/’ . $attachmentMetadata[‘file’];
#Checking image exists or not if not then no point of adding it into WP Offload Media’s table
if (!file_exists($fullFilePath)) {
continue;
}
print_r($attachmentMetadata);
#preparing data
$data = [
‘provider’ => ‘aws’,
‘region’ => $offloadSettings[‘region’],
‘bucket’ => $offloadSettings[‘bucket’],
‘path’ => $objectPrefix . ‘/’ . $attachment->attached_file,
‘original_path’ => $objectPrefix . ‘/’ . $attachment->attached_file,
‘is_private’ => 0,
‘source_type’ => ‘media-library’,
‘source_id’ => $attachment->ID,
‘source_path’ => $attachment->attached_file,
‘original_source_path’ => $attachment->attached_file,
‘extra_info’ => ‘a:2:{s:13:"private_sizes";a:0:{}s:14:"private_prefix";s:0:"";}’,
‘originator’ => 0,
‘is_verified’ => 1
];
ksort($data);
#Adding record into WP Offload Media’s table
$wpdb->insert("{$wpdb->prefix}{$itemsTable}", $data, $format);
}
update_option(‘_site_transient_wh_last_sync_attachment’, $lastAttachmentID, false);
}
以上功能经过全面测试,完美运行
参考:How to setup free CDN for WordPress using AWS S3
希望这会有所帮助!
【讨论】:
我应该在主题的功能中添加代码在哪里?
正如您在链接中看到的那样,我迁移到多站点设置并选择将所有图像卸载到 S3,有些去了,有些没有。
https://samba.tours/2022/01/20/saci-perere-brazilian-folklore/
感谢您的帮助!
【讨论】: