Search in sources :

Example 1 with CannedAccessControlList

use of com.aliyun.oss.model.CannedAccessControlList in project hadoop by apache.

the class AliyunOSSFileSystemStore method initialize.

public void initialize(URI uri, Configuration conf, FileSystem.Statistics stat) throws IOException {
    statistics = stat;
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS_KEY, MAXIMUM_CONNECTIONS_DEFAULT));
    boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS_KEY, SECURE_CONNECTIONS_DEFAULT);
    clientConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP);
    clientConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES_KEY, MAX_ERROR_RETRIES_DEFAULT));
    clientConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT_KEY, ESTABLISH_TIMEOUT_DEFAULT));
    clientConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT_KEY, SOCKET_TIMEOUT_DEFAULT));
    String proxyHost = conf.getTrimmed(PROXY_HOST_KEY, "");
    int proxyPort = conf.getInt(PROXY_PORT_KEY, -1);
    if (StringUtils.isNotEmpty(proxyHost)) {
        clientConf.setProxyHost(proxyHost);
        if (proxyPort >= 0) {
            clientConf.setProxyPort(proxyPort);
        } else {
            if (secureConnections) {
                LOG.warn("Proxy host set without port. Using HTTPS default 443");
                clientConf.setProxyPort(443);
            } else {
                LOG.warn("Proxy host set without port. Using HTTP default 80");
                clientConf.setProxyPort(80);
            }
        }
        String proxyUsername = conf.getTrimmed(PROXY_USERNAME_KEY);
        String proxyPassword = conf.getTrimmed(PROXY_PASSWORD_KEY);
        if ((proxyUsername == null) != (proxyPassword == null)) {
            String msg = "Proxy error: " + PROXY_USERNAME_KEY + " or " + PROXY_PASSWORD_KEY + " set without the other.";
            LOG.error(msg);
            throw new IllegalArgumentException(msg);
        }
        clientConf.setProxyUsername(proxyUsername);
        clientConf.setProxyPassword(proxyPassword);
        clientConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN_KEY));
        clientConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION_KEY));
    } else if (proxyPort >= 0) {
        String msg = "Proxy error: " + PROXY_PORT_KEY + " set without " + PROXY_HOST_KEY;
        LOG.error(msg);
        throw new IllegalArgumentException(msg);
    }
    String endPoint = conf.getTrimmed(ENDPOINT_KEY, "");
    CredentialsProvider provider = AliyunOSSUtils.getCredentialsProvider(conf);
    ossClient = new OSSClient(endPoint, provider, clientConf);
    uploadPartSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    multipartThreshold = conf.getLong(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY, MIN_MULTIPART_UPLOAD_THRESHOLD_DEFAULT);
    partSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    if (partSize < MIN_MULTIPART_UPLOAD_PART_SIZE) {
        partSize = MIN_MULTIPART_UPLOAD_PART_SIZE;
    }
    serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM_KEY, "");
    if (uploadPartSize < 5 * 1024 * 1024) {
        LOG.warn(MULTIPART_UPLOAD_SIZE_KEY + " must be at least 5 MB");
        uploadPartSize = 5 * 1024 * 1024;
    }
    if (multipartThreshold < 5 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be at least 5 MB");
        multipartThreshold = 5 * 1024 * 1024;
    }
    if (multipartThreshold > 1024 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be less than 1 GB");
        multipartThreshold = 1024 * 1024 * 1024;
    }
    String cannedACLName = conf.get(CANNED_ACL_KEY, CANNED_ACL_DEFAULT);
    if (StringUtils.isNotEmpty(cannedACLName)) {
        CannedAccessControlList cannedACL = CannedAccessControlList.valueOf(cannedACLName);
        ossClient.setBucketAcl(bucketName, cannedACL);
    }
    maxKeys = conf.getInt(MAX_PAGING_KEYS_KEY, MAX_PAGING_KEYS_DEFAULT);
    bucketName = uri.getHost();
}
Also used : OSSClient(com.aliyun.oss.OSSClient) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 2 with CannedAccessControlList

use of com.aliyun.oss.model.CannedAccessControlList in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method setObjectAcl.

public void setObjectAcl(SetObjectAclRequest setObjectAclRequest) throws OSSException, ClientException {
    assertParameterNotNull(setObjectAclRequest, "setObjectAclRequest");
    String bucketName = setObjectAclRequest.getBucketName();
    String key = setObjectAclRequest.getKey();
    CannedAccessControlList cannedAcl = setObjectAclRequest.getCannedACL();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertParameterNotNull(cannedAcl, "cannedAcl");
    Map<String, String> headers = new HashMap<String, String>();
    headers.put(OSSHeaders.OSS_OBJECT_ACL, cannedAcl.toString());
    Map<String, String> params = new HashMap<String, String>();
    params.put(SUBRESOURCE_ACL, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(key).setParameters(params).setHeaders(headers).setOriginalRequest(setObjectAclRequest).build();
    doOperation(request, emptyResponseParser, bucketName, key);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList)

Example 3 with CannedAccessControlList

use of com.aliyun.oss.model.CannedAccessControlList in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseGetBucketInfo.

/**
 * Unmarshall get bucket info response body to bucket info.
 */
public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        Element bucketElem = root.getChild("Bucket");
        BucketInfo bucketInfo = new BucketInfo();
        // owner
        Bucket bucket = new Bucket();
        String id = bucketElem.getChild("Owner").getChildText("ID");
        String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        bucket.setOwner(owner);
        // bucket
        bucket.setName(bucketElem.getChildText("Name"));
        bucket.setLocation(bucketElem.getChildText("Location"));
        bucket.setExtranetEndpoint(bucketElem.getChildText("ExtranetEndpoint"));
        bucket.setIntranetEndpoint(bucketElem.getChildText("IntranetEndpoint"));
        bucket.setCreationDate(DateUtil.parseIso8601Date(bucketElem.getChildText("CreationDate")));
        if (bucketElem.getChild("StorageClass") != null) {
            bucket.setStorageClass(StorageClass.parse(bucketElem.getChildText("StorageClass")));
        }
        bucketInfo.setBucket(bucket);
        // acl
        String aclString = bucketElem.getChild("AccessControlList").getChildText("Grant");
        CannedAccessControlList acl = CannedAccessControlList.parse(aclString);
        bucketInfo.setCannedACL(acl);
        switch(acl) {
            case PublicRead:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.Read);
                break;
            case PublicReadWrite:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
                break;
            default:
                break;
        }
        return bucketInfo;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Owner(com.aliyun.oss.model.Owner) Bucket(com.aliyun.oss.model.Bucket) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) BucketInfo(com.aliyun.oss.model.BucketInfo) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 4 with CannedAccessControlList

use of com.aliyun.oss.model.CannedAccessControlList in project aliyun-oss-java-sdk by aliyun.

the class ObjectAclTest method testNormalSetObjectAcl.

@Test
public void testNormalSetObjectAcl() {
    final String key = "normal-set-object-acl";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ossClient.putObject(bucketName, key, instream);
        for (CannedAccessControlList acl : ACLS) {
            ossClient.setObjectAcl(bucketName, key, acl);
            ObjectAcl returnedAcl = ossClient.getObjectAcl(bucketName, key);
            Assert.assertEquals(acl.toString(), returnedAcl.getPermission().toString());
            Assert.assertEquals(returnedAcl.getRequestId().length(), REQUEST_ID_LEN);
            OSSObject object = ossClient.getObject(bucketName, key);
            Assert.assertEquals(inputStreamLength, object.getObjectMetadata().getContentLength());
            Assert.assertEquals(object.getRequestId().length(), REQUEST_ID_LEN);
            object.getObjectContent().close();
        }
        // Set to default acl again
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
        ObjectAcl returnedAcl = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.Default, returnedAcl.getPermission());
        Assert.assertEquals(returnedAcl.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with CannedAccessControlList

use of com.aliyun.oss.model.CannedAccessControlList in project aliyun-oss-java-sdk by aliyun.

the class BucketAclTest method testNormalSetBucketAcl.

@Test
public void testNormalSetBucketAcl() {
    final String bucketName = "normal-set-bucket-acl";
    try {
        ossClient.createBucket(bucketName);
        for (CannedAccessControlList acl : acls) {
            ossClient.setBucketAcl(bucketName, acl);
            AccessControlList returnedAcl = ossClient.getBucketAcl(bucketName);
            if (acl != null && !acl.equals(CannedAccessControlList.Private)) {
                Set<Grant> grants = returnedAcl.getGrants();
                Assert.assertEquals(1, grants.size());
                Grant grant = (Grant) grants.toArray()[0];
                if (acl.equals(CannedAccessControlList.PublicRead)) {
                    Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
                    Assert.assertEquals(Permission.Read, grant.getPermission());
                } else if (acl.equals(CannedAccessControlList.PublicReadWrite)) {
                    Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
                    Assert.assertEquals(Permission.FullControl, grant.getPermission());
                }
            }
            Assert.assertEquals(returnedAcl.getRequestId().length(), REQUEST_ID_LEN);
            if (acl != null) {
                Assert.assertEquals(returnedAcl.getCannedACL(), acl);
            }
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)6 OSSException (com.aliyun.oss.OSSException)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)2 AccessControlList (com.aliyun.oss.model.AccessControlList)2 Owner (com.aliyun.oss.model.Owner)2 ParseException (java.text.ParseException)2 Element (org.jdom.Element)2 JDOMParseException (org.jdom.input.JDOMParseException)2 Test (org.junit.Test)2 ClientConfiguration (com.aliyun.oss.ClientConfiguration)1 OSSClient (com.aliyun.oss.OSSClient)1 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)1 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)1 Bucket (com.aliyun.oss.model.Bucket)1 BucketInfo (com.aliyun.oss.model.BucketInfo)1 Grant (com.aliyun.oss.model.Grant)1 OSSObject (com.aliyun.oss.model.OSSObject)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1