我有一个指向 S3 存储桶的映射本地驱动器 (X:)。
我使用以下代码将上传的图像推送到其中。
public void copyData( MultipartFile file, String dest ) throws RecoverableFileApiException {
try {
String path = FilenameUtils.getFullPath( dest );
File fileDir = new File( path );
String fileName = FilenameUtils.getName( dest );
File outFile = new File( fileDir, fileName );
if ( !fileDir.exists() ) {
FileUtils.forceMkdir( fileDir );
}
file.transferTo(outFile);
FileUtils.touch( outFile );
//fileDir.setLastModified( System.currentTimeMillis() );
}
catch ( IOException e ) {
throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
}
}
它会创建相关目录 (X:\uploads\category1) 并在第一次上传文件时将文件放入其中。文件图像已正确提供(可从浏览器查看)。
当我添加新图像时,它们会被放入同一目录(X:\uploads\category1)中,并且图像也正常。
但图像似乎没有被推送到边缘服务器。
当我通过浏览器查看它时,它会下载图像而不是显示。
在 Chrome 网络选项卡中,我可以看到它被用作内容长度为 0 的“二进制/八位组流”内容类型。
就好像 aws 文件夹需要在目录级别上启动才能让它们拾取添加到目录中的新文件?
当我通过手动将图像复制/粘贴到目录中来执行相同操作时,它可以很好地处理它们。
暂时不考虑使用 AWS API,除非没有它们就无法工作。
最佳答案
按照 John 的建议,将在稍后阶段使用 API 实现。
目前,通过以下方式使其正常工作:
- 使用临时名称在本地驱动器上创建文件
- 将文件复制到映射驱动器
- 将映射驱动器上的文件重命名为正确的名称。
- 完成,被推送到边缘服务器。
重命名映射驱动器(S3 存储桶)上的文件会强制在下次有人请求时提供新镜像。
public void copyDataToCDN( InputStream is, String dest ) throws RecoverableFileApiException {
FileOutputStream os = null;
File tmpLocalFile = null;
try {
String path = FilenameUtils.getFullPath( dest );
File fileDir = new File( path );
String fileName = FilenameUtils.getName( dest );
File outFile = new File( fileDir, fileName );
if ( !fileDir.exists() ) {
FileUtils.forceMkdir( fileDir );
}
if ( outFile.exists() ) {
outFile.delete();
}
// Temporarily copy to local file system
String tmpLocalDir = System.getProperty( "java.io.tmpdir" );
String tmpDest = tmpLocalDir + "temp_" + fileName;
String tmpPath = FilenameUtils.getFullPath( tmpDest );
File tmpFileDir = new File( tmpPath );
String tmpFileName = FilenameUtils.getName( tmpDest );
tmpLocalFile = new File( tmpFileDir, tmpFileName );
os = new FileOutputStream( tmpLocalFile );
IOUtils.copy( is, os );
// Copy temp file to Mapped CDN Drive
String tmpCDNFileName = "temp_" + fileName;
File tmpCDNFile = new File( fileDir, tmpCDNFileName );
FileUtils.copyFile( tmpLocalFile, tmpCDNFile );
// Rename temp file on Mapped CDN Drives
File finalCDNFile = new File( fileDir, fileName );
tmpCDNFile.renameTo( finalCDNFile );
}
catch ( IOException e ) {
throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
}
finally {
IOUtils.closeQuietly( is );
IOUtils.closeQuietly( os );
if ( tmpLocalFile != null ) {
tmpLocalFile.delete();
}
}
}
关于java - 上传图像到 AWS S3 未推送到边缘服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62189041/