简体   繁体   English

有没有办法在用 Java 创建文件时将文件发送到 AWS S3 存储桶?

[英]Is there a way to send a file to an AWS S3 Bucket while creating it in Java?

I have incominig Data from a Database, which should be written to a CSV-file which again should be stored in a Amazon S3 Bucket.我有来自数据库的传入数据,应将其写入 CSV 文件,该文件应再次存储在 Amazon S3 存储桶中。 Im not allowed to use very much of my local storage (about 1GB).我不允许使用很多本地存储(大约 1GB)。 Is it possible to upload the incoming data as a CSV-file without using local storage or with just using that little i have?是否可以将传入的数据作为 CSV 文件上传而不使用本地存储或仅使用我拥有的那一点? The file will have more than 10 GB.该文件将超过 10 GB。

It's pretty easy to do with AWS SDK, but the crucial thing is you need to know the file size before starting the upload使用 AWS SDK 很容易做到,但关键是您需要在开始上传之前知道文件大小

If you know, how big the file will be, then you can prepare your own InputStream and pass it to the S3 client like this:如果您知道文件有多大,那么您可以准备自己的 InputStream 并将其传递给 S3 客户端,如下所示:

public class DynamicUpload {

    public static void main(String[] args) {
        // Create S3 client
        AmazonS3 s3 = AmazonS3Client.builder().withRegion("eu-central-1").build();
        CsvStream stream = new CsvStream();
        // When providing InputStream, you must set content length
        ObjectMetadata obj = new ObjectMetadata();
        obj.setContentLength(stream.getSize());
        obj.setContentType("text/plain");
        // Pass created InputStream as a source
        s3.putObject(new PutObjectRequest("files.stirante.com", "stackOverflow.csv", stream, obj));
    }

    private static class CsvStream extends InputStream {

        private static DecimalFormat format = new DecimalFormat("00");
        // Target size for testing purposes
        private int size = 100000;
        // This is size of one row "XX;XX;XX\n"
        private int itemSize = 9;
        // Since we increment it at the very beginning, we set it to -1
        private int currentItemIndex = -1;
        // Current row, we're returning
        private byte[] currentItem = null;
        // Byte index in current row
        private int currentItemByteIndex = 0;

        /**
         * Returns size of the whole file
         */
        public int getSize() {
            return size * itemSize;
        }

        @Override
        public int read() throws IOException {
            // Every time read is called, we return another character from created earlier row
            currentItemByteIndex++;
            // If row is not initialized or earlier row was already fully returned, we create another row
            if (currentItem == null || currentItemByteIndex >= itemSize) {
                currentItemIndex++;
                // If we don't have another row, we throw end of file exception
                if (currentItemIndex == size) {
                    throw new EOFException();
                }
                // Format guarantees us, that in case of number smaller than 10, it will still return 2 characters (e.g. 02)
                String s = format.format(Math.random() * 99) + ";" +
                        format.format(Math.random() * 99) + ";" +
                        format.format(Math.random() * 99) + "\n";
                currentItem = s.getBytes();
                currentItemByteIndex = 0;
            }
            return currentItem[currentItemByteIndex];
        }
    }
}

Example generated file示例生成文件

Documentation: PutObjectRequest文档: PutObjectRequest

I see 2 ways to achieve that:我看到了两种方法来实现这一目标:

  1. Use multi part file uploading by handling 1 GB per part locally.通过在本地处理每部分 1 GB 来使用多部分文件上传。
    See example .参见示例
  2. Use cloud instance instead of local one (where you don't have 1 GB limitation) and use atomic upload (eg AmazonS3Client#putObject method)使用云实例而不是本地实例(您没有 1 GB 的限制)并使用原子上传(例如 AmazonS3Client#putObject 方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 上载到AWS S3存储桶时更改文件名 - Changing file name while uploading to AWS S3 bucket 使用 Java 从 S3 存储桶和 HTTP PUT 文件读取文件到预签名的 AWS S3 URL 以模拟实际文件上传的方式另一个存储桶 - Read a file using Java from an S3 bucket and HTTP PUT file to presigned AWS S3 URL of another bucket in a way that simulates an actual file upload AWS Lambda Java,写入 S3 存储桶 - AWS Lambda Java, write to S3 bucket 如何将文件上传到AWS S3 Bucket? - How to upload a file to AWS S3 Bucket? 使用 AWS SDK 为 Java 创建 Amazon S3 存储桶:线程“主”java.lang.NoClassDefFoundError 中的异常 - Creating an Amazon S3 bucket Using the AWS SDK for Java : Exception in thread "main" java.lang.NoClassDefFoundError 在Amazon S3存储桶中创建Avro文件 - Creating an Avro file in Amazon S3 bucket 从 Java/Spring 向 AWS S3 发送文件时出错 - Error to send a file to AWS S3 from Java/Spring 使用 aws-sdk-java 上传到 S3 存储桶时出现永久重定向错误 - PermanentRedirect error while uploading to S3 bucket with aws-sdk-java 有没有办法禁用 AWS java SDK 2.0 的标准输出日志记录。 用于 S3 存储桶上传 - Is there a way to disable stdout logging for AWS java SDK 2.0. for S3 Bucket upload java-通过html文件字段将图像文件上传到我的AWS S3存储桶中 - java - upload image file into my aws s3 bucket through html file field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM