Чтение файла с s3 и загрузка через SFTP-сервер Apache MINA - PullRequest
0 голосов
/ 04 мая 2020

Я пытаюсь прочитать содержимое файла с s3 и загрузить его на клиент SFTP через Apache сервер MINA SFTP. Я могу это сделать, но пропускаю меньше байтов между целым файлом, например: если размер файла составляет 13,7 МБ, то при загрузке кода ниже 13,4 МБ, сравнение обоих файлов показывает, что между ними есть некоторые пропущенные байты.

Код, используемый для получения файла из хранилища объектов:

public InputStream getFileFromBucket(String bucket, String key) {
        S3Object object = s3Client.getObject(new GetObjectRequest(bucket, key));
        InputStream objectData = object.getObjectContent();
        return objectData;
    }

Функция чтения файлового канала (Apache MINA):

xyzFileSystemProvider.class

public class xyzFileSystemProvider extends FileSystemProvider {
    @Override
        public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
                throws IOException {
            Collection<xyzOptions.OpenMode> modes = xyzOptions.OpenMode.fromOpenOptions(options);
            if (modes.isEmpty()) {
                modes = EnumSet.of(xyzOptions.OpenMode.Read, xyzOptions.OpenMode.Write);
            }
            return new xyzFileSystemChannel(toxyzPath(path), modes);
        }
}

XYZFileSystemChannel.class

public class xyzFileSystemChannel extends xyzRemotePathChannel {

    public xyzFileSystemChannel(xyzPath p, Collection<xyzOptions.OpenMode> modes) throws IOException {
            this(Objects.requireNonNull(p, "No target path").toString(), p.getFileSystem(), modes);
        }

        public xyzFileSystemChannel(String remotePath, xyzFileSystem fs, Collection<xyzOptions.OpenMode> modes)
                throws IOException {
            super(remotePath, fs, true, modes);
        }
}

xyzRemotePathChannel.class

public xyzRemotePathChannel(String path, xyzFileSystem fileSystem, boolean closeOnExit,
                Collection<xyzOptions.OpenMode> modes) throws IOException {
    if (modes.equals(READ_MODES)) {

                    this.fileStreamToRead = getAmazonS3Instance().getFileFromBucket(bucket, fileName);
                    this.fileSize = fileStreamToRead.available();
                }
    }

    @Override
        synchronized public int read(ByteBuffer dst) throws IOException {
            ensureOpen(READ_MODES);
            try {
                synchronized (dst) {
                    long curPos = posTracker.get();
                    int nMaxBytesToWrite = dst.limit() - dst.position();
                    byte[] bytesArr = new byte[nMaxBytesToWrite];
                    int actualBytesRead = fileStreamToRead.read(bytesArr, 0, nMaxBytesToWrite);
                    if (actualBytesRead > 0) {
                        dst.put(bytesArr, 0, actualBytesRead);
                        curPos += actualBytesRead;
                        posTracker.set(curPos);
                    }
                    return actualBytesRead;
                }
            } catch (Exception e) {
                log.error("Exception occured {}", e);
                return -1;
            } finally {
                dst.clear();
            }
        }

    @Override
        public FileChannel position(long newPosition) throws IOException {
            log.debug("Setting new position :{}", newPosition);
            if (!this.modes.equals(READ_MODES)) {
                this.posTracker.set(newPosition);
            }
            return this;
        }
}

Пожалуйста, дайте мне знать если я что-то упустил.

...