a5storage_savefile Function

Syntax

L a5Storage_saveFile(C connectionString ,C filename ,C itemName [,C mimeType [,P pResult [,B pDataIn [,L flagSetReadPermission [, flagOverwriteExistingObject = .t.]]]]])

Arguments

connectionStringCharacter

Storage connection string with ::storage:: as a prefix.

filenameCharacter

The name of the local file to save in storage. If set to "<BinaryData>", this function can be used to save a binary object instead. The binary data must be specified in the pDataIn parameter. It is strongly recommended that you use a5storage_saveData() to save binary data.

itemNameCharacter

The name of the object in storage. You can can specify a logical folder by using forward slashes in the name. For example: "image/image1.jpg"

mimeTypeCharacter

The mime type of the object. If you don't specify this property, the value can be inferred from the extension assigned to the itemName property.

pResultPointer

Default = null_value(). An optional dot variable that you can pass in that will be populated with information about the object.

pDataInBinary

Default = null_value(). A binary object containing data to save to file. This property is only used if filename is set to "<BinaryData>". It is strongly recommended that you use a5storage_saveData() to save binary data.

flagSetReadPermissionLogical

Default = .f.. If set to .t., adds read access to the file for everyone.

flagOverwriteExistingObjectLogical

Default = .t.. If .t., if an object already exists in the storage with the same name, the object is replaced by the uploaded file. Otherwise, the uploaded file is assigned a unique name. If the object is renamed and you pass in the optional pResult parameter, the actual object name that was used to save the new object is available in the pResult.actualItemName property.

Description

Saves a file to storage, including saving files to Amazon S3 or Azure

Required AWS S3 IAM Policy

When using a5storage_savefile() with Amazon S3, you must configure an IAM policy with the minimum required permissions. The following policy grants exactly the permissions needed for successful file uploads without over-permissioning.

To apply this policy:

1. Log into AWS IAM Console

2. Create or select an IAM user for Alpha Anywhere

3. Attach this policy to the user

4. Use the user's access key and secret key in your Alpha Anywhere storage connection string

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:GetBucketLocation",
                "s3:ListBucketMultipartUploads"
            ],
            "Resource": "arn:aws:s3:::<BUCKET-NAME>"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:GetObjectAcl",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::<BUCKET-NAME>/*"
        }
    ]
}
Replace <BUCKET-NAME> with your actual S3 bucket name in both Resource ARN statements.

Example:

dim pr as p

flag = a5Storage_saveFile("::storage::Amazon_East","c:\images\4290.jpg","movies/4290.jpg","",pr)

? flag
= .T.

If you examine the pr dot variable that was passed into the function you will see the following properties:

hasError = .f.
timeTakenMilliseconds  = 239
AbsolutePath  = "https://.s3.amazonaws.com/movies/4290.jpg"
ContentType  = "image/jpeg"
ModifiedTime  = CTODT('03/15/2014 01:53:13 00 pm')
Name  = "movies/4290.jpg"
size  = 5880

The AbsolutePath property gives you a URL to the object. Note that in order for this URL to work you need to make sure that you have set the appropriate permissions on the storage container ('bucket' in S3 terminology).

See Also