|
| 1 | +/* |
| 2 | + * **************************************************************************** |
| 3 | + * Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | + * specific language governing permissions and limitations under the License. |
| 13 | + * **************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +package com.spectralogic.ds3client.samples; |
| 17 | + |
| 18 | +import com.spectralogic.ds3client.Ds3Client; |
| 19 | +import com.spectralogic.ds3client.Ds3ClientBuilder; |
| 20 | +import com.spectralogic.ds3client.commands.PutObjectRequest; |
| 21 | +import com.spectralogic.ds3client.commands.PutObjectResponse; |
| 22 | +import com.spectralogic.ds3client.commands.spectrads3.GetBucketSpectraS3Request; |
| 23 | +import com.spectralogic.ds3client.commands.spectrads3.GetBucketSpectraS3Response; |
| 24 | +import com.spectralogic.ds3client.helpers.Ds3ClientHelpers; |
| 25 | +import com.spectralogic.ds3client.models.Contents; |
| 26 | +import com.spectralogic.ds3client.networking.FailedRequestException; |
| 27 | +import com.spectralogic.ds3client.networking.FailedRequestUsingMgmtPortException; |
| 28 | +import com.spectralogic.ds3client.utils.EmptySeekableByteChannel; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.net.UnknownHostException; |
| 32 | +import java.security.SignatureException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Put a zero-byte object with a trailing slash to serve as an empty folder. |
| 36 | + * Use the Standard Amazon S3 Put Object (does not require a Job) |
| 37 | + * |
| 38 | + * NOTE: This PutObjectRequest construstor is marked deprecated |
| 39 | + * This is to drive development toward Spectra S3 calls; support for this will not be removed |
| 40 | + */ |
| 41 | +public class PutEmptyFolderExample { |
| 42 | + |
| 43 | + public static void main(final String args[]) throws IOException, SignatureException { |
| 44 | + |
| 45 | + if (args.length < 2) { |
| 46 | + System.out.println("Usage: bucketname foldername"); |
| 47 | + return; |
| 48 | + } |
| 49 | + final String bucketname = args[0]; |
| 50 | + String foldername = args[1]; |
| 51 | + |
| 52 | + // an object name with trailing / is treated as a folder |
| 53 | + if (!foldername.endsWith("/")) { |
| 54 | + foldername += "/"; |
| 55 | + } |
| 56 | + |
| 57 | + // Get a client builder and then build a client instance. This is the main entry point to the SDK. |
| 58 | + try (final Ds3Client client = Ds3ClientBuilder.fromEnv().withHttps(false).build()) { |
| 59 | + |
| 60 | + // Put using EmptySeekableByteChannel, zero-length |
| 61 | + final PutObjectResponse putObjectResponse = client.putObject(new PutObjectRequest(bucketname, foldername, new EmptySeekableByteChannel(), 0L)); |
| 62 | + |
| 63 | + // New folder can be accessed by Standard S3 or Spectra calls |
| 64 | + final Ds3ClientHelpers helper = Ds3ClientHelpers.wrap(client); |
| 65 | + final Iterable<Contents> objects = helper.listObjects(bucketname, foldername); |
| 66 | + for (final Contents object : objects) { |
| 67 | + System.out.println("Created folder: " + object.getKey() ); |
| 68 | + } |
| 69 | + |
| 70 | + // Catch unknown host exceptions. |
| 71 | + } catch (final UnknownHostException e) { |
| 72 | + |
| 73 | + System.out.println("Invalid Endpoint Server Name or IP Address"); |
| 74 | + |
| 75 | + // Catch unknown host exceptions. |
| 76 | + } catch (final FailedRequestUsingMgmtPortException e) { |
| 77 | + |
| 78 | + System.out.println("Attempted data access on management port -- check endpoint"); |
| 79 | + |
| 80 | + // Catch failed requests with unexpected status codes. |
| 81 | + } catch (final FailedRequestException e) { |
| 82 | + |
| 83 | + // If this is invalid authorization. |
| 84 | + if (e.getStatusCode() == 403) { |
| 85 | + System.out.println("ERROR: Invalid Access ID or Secret Key"); |
| 86 | + |
| 87 | + } else if (e.getStatusCode() == 409) { |
| 88 | + System.out.println("ERROR: Object Exists"); |
| 89 | + |
| 90 | + // Else unexpected status code. |
| 91 | + } else { |
| 92 | + System.out.println("BlackPearl returned an unexpected status code"); |
| 93 | + } |
| 94 | + |
| 95 | + } catch (final IOException e) { |
| 96 | + |
| 97 | + System.out.println("Encountered a networking error"); |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments