Skip to content

Commit

Permalink
Merge pull request #23 from lahsivjar/issue-19
Browse files Browse the repository at this point in the history
Handle small files in a single blob request
  • Loading branch information
lahsivjar authored Mar 11, 2019
2 parents 085a23d + ea4642f commit a03c38d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ Hash based project id resolution scheme supports environment variable interpolat
Report any issues or bugs to https://github.com/lahsivjar/gcp-storage-wagon/issues

## Changelog
### 2.1
* (Bugfix #19) Handle small files in single blob request

### 2.0
* Environment variable interpolation for hash separated project id resolution

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.lahsivjar</groupId>
<artifactId>gcp-storage-wagon</artifactId>
<version>2.0</version>
<version>2.1</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Maven wagon provider for google cloud storage</description>
Expand Down
52 changes: 38 additions & 14 deletions src/main/java/com/lahsivjar/GcpStorageWagon.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -135,23 +136,46 @@ public void put(File source, String destination)
.build();
firePutStarted(resource, source);
final TransferEvent transferProgressEvent = buildTransferProgressEvent(resource, TransferEvent.REQUEST_PUT);
try (final WriteChannel writer = this.storage.writer(blobInfo)) {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try (InputStream input = new FileInputStream(source)) {
int limit;
while ((limit = input.read(buffer)) >= 0) {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
fireTransferProgress(transferProgressEvent, buffer, limit);
}
}
} catch (FileNotFoundException fe) {
throw new ResourceDoesNotExistException(String.format("%s does not exist", source.getName()), fe);

if (!source.exists()) {
throw new ResourceDoesNotExistException(String.format("%s does not exist", source.getName()));
}

long fileSize;
try {
fileSize = Files.size(source.toPath());
} catch (IOException e) {
throw new TransferFailedException(String.format("Failed to transfer %s to %s",
source.getName(), destination), e);
} catch (StorageException se) {
throw new TransferFailedException(String.format("Failed to transfer %s to %s",
source.getName(), destination), se);
}

if (fileSize > 1_000_000) {
try (final WriteChannel writer = this.storage.writer(blobInfo)) {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try (InputStream input = new FileInputStream(source)) {
int limit;
while ((limit = input.read(buffer)) >= 0) {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
fireTransferProgress(transferProgressEvent, buffer, limit);
}
}
} catch (IOException e) {
throw new TransferFailedException(String.format("Failed to transfer %s to %s",
source.getName(), destination), e);
} catch (StorageException se) {
throw new TransferFailedException(String.format("Failed to transfer %s to %s",
source.getName(), destination), se);
}
} else {
byte[] bytes;
try {
bytes = Files.readAllBytes(source.toPath());
} catch (IOException e) {
throw new TransferFailedException(String.format("Failed to transfer %s to %s",
source.getName(), destination), e);
}
this.storage.create(blobInfo, bytes);
fireTransferProgress(transferProgressEvent, bytes, bytes.length);
}
firePutCompleted(resource, source);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/lahsivjar/GcpStorageWagonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
import com.google.common.io.Files;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
Expand Down Expand Up @@ -54,6 +55,11 @@ private void writeContentToFile(File file) throws IOException {
}
}

private void writeContentToFile(File file, int size) throws IOException {
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.setLength(size);
}

private void putFileUtil(GcpStorageWagon storageWagon, String destinationPath) throws IOException, ConnectionException,
AuthenticationException, AuthorizationException, ResourceDoesNotExistException, TransferFailedException {
final File sourceFile = sourceFolder.newFile(DUMMY_FILE_NAME);
Expand Down Expand Up @@ -108,6 +114,22 @@ public void testPut() throws IOException, ConnectionException, AuthenticationExc
Assert.assertTrue(blob.exists());
}

@Test
public void testPut_fileGreaterThan1MB() throws IOException, ConnectionException, AuthenticationException,
AuthorizationException, ResourceDoesNotExistException, TransferFailedException {
final Storage storage = fakeStorage();
final GcpStorageWagon storageWagon = new GcpStorageWagon(storage);
final File sourceFile = sourceFolder.newFile(DUMMY_FILE_NAME);

writeContentToFile(sourceFile, 1024 * 1024);
storageWagon.connect(fakeRepository());
storageWagon.put(sourceFile, DUMMY_FILE_NAME);

Blob blob = storage.get(BlobId.of(DUMMY_BUCKET, DUMMY_BASE_DIR + DUMMY_FILE_NAME));
Assert.assertNotNull(blob);
Assert.assertTrue(blob.exists());
}

@Test(expected = ResourceDoesNotExistException.class)
public void testPutNoResource() throws IOException, ConnectionException, AuthenticationException,
AuthorizationException, ResourceDoesNotExistException, TransferFailedException {
Expand Down

0 comments on commit a03c38d

Please sign in to comment.