【问题标题】:Uploaded images to AWS S3 not pushing to edge servers将图像上传到 AWS S3 而不推送到边缘服务器
【发布时间】:2020-06-04 07:27:13
【问题描述】:

我有一个指向 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,除非当然没有它们就无法开始工作。

【问题讨论】:

  • “指向 S3 存储桶的映射本地驱动器 (X:)”是什么意思? X:如何与 S3 关联?
  • @JohnRotenstein 使用 TNTDrive 将可移动驱动器映射到 S3 存储桶。 tntdrive.com
  • 嗯,听起来你的问题出在 TNTDrive 上。 Amazon S3 是一个对象存储系统,应该通过 API 访问。不建议将 S3 作为文件系统“挂载”,尤其是在生产环境中使用。

标签: java amazon-s3 fileutils


【解决方案1】:

按照 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();
      }
    }
  }

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2017-01-29
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多