165 lines
No EOL
5.5 KiB
Markdown
165 lines
No EOL
5.5 KiB
Markdown
# frostfs-sdk-java
|
|
|
|
Java implementation of FrostFS SDK
|
|
|
|
## Prerequisites
|
|
|
|
### Get the key for your wallet
|
|
|
|
1. Get the address
|
|
```bash
|
|
cat <path_to_your_wallet> | jq .accounts[0].address | tr -d '"'
|
|
```
|
|
|
|
2. Get the key
|
|
```bash
|
|
neo-go wallet export -w <path_to_your_wallet> -d <address_from_p1>
|
|
```
|
|
|
|
## Example usage
|
|
|
|
### Container operations
|
|
|
|
```java
|
|
import info.frostfs.sdk.FrostFSClient;
|
|
import info.frostfs.sdk.dto.container.Container;
|
|
import info.frostfs.sdk.dto.netmap.PlacementPolicy;
|
|
import info.frostfs.sdk.dto.netmap.Replica;
|
|
import info.frostfs.sdk.enums.BasicAcl;
|
|
import info.frostfs.sdk.jdo.ClientSettings;
|
|
import info.frostfs.sdk.jdo.parameters.CallContext;
|
|
import info.frostfs.sdk.jdo.parameters.container.PrmContainerCreate;
|
|
import info.frostfs.sdk.jdo.parameters.container.PrmContainerDelete;
|
|
import info.frostfs.sdk.jdo.parameters.container.PrmContainerGet;
|
|
import info.frostfs.sdk.jdo.parameters.container.PrmContainerGetAll;
|
|
|
|
public class ContainerExample {
|
|
|
|
public void example() {
|
|
var callContext = new CallContext();
|
|
ClientSettings clientSettings = new ClientSettings(<your_key>, <your_host>);
|
|
FrostFSClient frostFSClient = new FrostFSClient(clientSettings);
|
|
|
|
// Create container
|
|
var placementPolicy = new PlacementPolicy(new Replica[]{new Replica(1)}, true);
|
|
var prmContainerCreate = new PrmContainerCreate(new Container(BasicAcl.PUBLIC_RW, placementPolicy));
|
|
var containerId = frostFSClient.createContainer(prmContainerCreate, callContext);
|
|
|
|
// Get container
|
|
var prmContainerGet = new PrmContainerGet(containerId);
|
|
var container = frostFSClient.getContainer(prmContainerGet, callContext);
|
|
|
|
// List containers
|
|
var containerIds = frostFSClient.listContainers(new PrmContainerGetAll(), callContext);
|
|
|
|
// Delete container
|
|
var prmContainerDelete = new PrmContainerDelete(containerId);
|
|
frostFSClient.deleteContainer(prmContainerDelete, callContext);
|
|
}
|
|
}
|
|
```
|
|
|
|
### Object operations
|
|
|
|
```java
|
|
import info.frostfs.sdk.dto.object.*;
|
|
import info.frostfs.sdk.enums.ObjectType;
|
|
import info.frostfs.sdk.jdo.ClientSettings;
|
|
import info.frostfs.sdk.jdo.parameters.CallContext;
|
|
import info.frostfs.sdk.jdo.parameters.object.*;
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import static java.util.Objects.isNull;
|
|
|
|
public class ObjectExample {
|
|
|
|
public void example() {
|
|
CallContext callContext = new CallContext();
|
|
ClientSettings clientSettings = new ClientSettings(<your_key>, <your_host>);
|
|
FrostFSClient frostFSClient = new FrostFSClient(clientSettings);
|
|
|
|
// Put object
|
|
ObjectId objectId;
|
|
try (FileInputStream file = new FileInputStream("/path/to/file/cat.jpg")) {
|
|
var attribute = new ObjectAttribute("Filename", "cat.jpg");
|
|
var cat = new ObjectHeader(containerId, ObjectType.REGULAR, attribute);
|
|
var prmObjectPut = PrmObjectPut.builder().objectHeader(cat).build();
|
|
var writer = frostFSClient.putObject(prmObjectPut, callContext);
|
|
|
|
writer.write(file.readAllBytes());
|
|
objectId = writer.complete();
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
// Get object
|
|
var prmObjectGet = new PrmObjectGet(containerId, oid);
|
|
ObjectFrostFS object = frostFSClient.getObject(prmObjectGet, callContext);
|
|
|
|
var reader = object.getObjectReader();
|
|
var chunk = reader.readChunk();
|
|
var length = chunk.length;
|
|
byte[] buffer = null;
|
|
while (length > 0) {
|
|
buffer = isNull(buffer) ? chunk : ArrayHelper.concat(buffer, chunk);
|
|
|
|
chunk = object.getObjectReader().readChunk();
|
|
length = ArrayUtils.isEmpty(chunk) ? 0 : chunk.length;
|
|
}
|
|
|
|
try (FileOutputStream fos = new FileOutputStream("/path/to/file/newCat.jpg")) {
|
|
fos.write(buffer);
|
|
} catch (Exception ignored) {
|
|
}
|
|
|
|
// Get object header
|
|
var prmObjectHeadGet = new PrmObjectHeadGet(containerId, objectId);
|
|
var objectHeader = frostFSClient.getObjectHead(prmObjectHeadGet, callContext);
|
|
|
|
// Search regular objects
|
|
var prmObjectSearch = new PrmObjectSearch(containerId, new ObjectFilter.FilterByRootObject());
|
|
var objectIds = frostFSClient.searchObjects(prmObjectSearch, callContext);
|
|
|
|
// Delete object
|
|
var prmObjectDelete = new PrmObjectDelete(containerId, objectId);
|
|
frostFSClient.deleteObject(prmObjectDelete, callContext);
|
|
}
|
|
}
|
|
```
|
|
|
|
### Pool init
|
|
|
|
```java
|
|
import info.frostfs.sdk.jdo.ECDsa;
|
|
import info.frostfs.sdk.jdo.parameters.CallContext;
|
|
import info.frostfs.sdk.jdo.pool.NodeParameters;
|
|
import info.frostfs.sdk.jdo.pool.PoolInitParameters;
|
|
import info.frostfs.sdk.pool.Pool;
|
|
|
|
public class PoolExample {
|
|
|
|
public static void example() {
|
|
CallContext callContext = new CallContext();
|
|
|
|
//Init
|
|
var nodeParam1 = new NodeParameters(1, <your_host1>, 1);
|
|
var nodeParam2 = new NodeParameters(1, <your_host2>, 1);
|
|
var nodeParam3 = new NodeParameters(1, <your_host3>, 1);
|
|
var nodeParam4 = new NodeParameters(1, <your_host4>, 1);
|
|
|
|
PoolInitParameters initParameters = new PoolInitParameters();
|
|
initParameters.setKey(new ECDsa(<your_key>));
|
|
initParameters.setNodeParams(new NodeParameters[]{nodeParam1, nodeParam2, nodeParam3, nodeParam4});
|
|
|
|
|
|
Pool pool = new Pool(initParameters);
|
|
|
|
//Dial (Required!)
|
|
pool.dial(callContext);
|
|
}
|
|
}
|
|
``` |