Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rackspace cloud #46

Merged
merged 5 commits into from
Jan 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,49 @@ Slingshot.createDirective("google-cloud-example", Slingshot.GoogleCloud, {
});
```

### Rackspace Cloud Files

You will need a`RackspaceAccountId` (your acocunt number) and
`RackspaceMetaDataKey` in `Meteor.settings`.

In order to obtain your `RackspaceMetaDataKey` (a.k.a. Account-Meta-Temp-Url-Key)
you need an
[auth-token](http://docs.rackspace.com/loadbalancers/api/v1.0/clb-getting-started/content/Generating_Auth_Token.html)
and then follow the
[instructions here](http://docs.rackspace.com/files/api/v1/cf-devguide/content/Set_Account_Metadata-d1a666.html).

Note that API-Key, Auth-Token, Meta-Data-Key are not the same thing:

API-Key is what you need to obtain an Auth-Token, which in turn is what you need
to setup CORS and to set your Meta-Data-Key. The auth-token expires after 24 hours.

For your directive you need container and provide its name, region and cdn.

```JavaScript
Slingshot.createDirective("rackspace-files-example", Slingshot.RackspaceFiles, {
container: "myContainer", //Container name
region: "lon3", //Region code (The default would be 'iad3')

//You must set the cdn if you want the files to be publicly accessible:
cdn: "https://abcdefghije8c9d17810-ef6d926c15e2b87b22e15225c32e2e17.r19.cf5.rackcdn.com",

pathPrefix: function (file) {
//Store file into a directory by the user's username.
var user = Meteor.users.findOne(this.userId);
return user.username;
}
});
```

To setup CORS you also need to your Auth-Token from above and use:

```bash
curl -I -X POST -H 'X-Auth-Token: yourAuthToken' \
-H 'X-Container-Meta-Access-Control-Allow-Origin: *' \
-H 'X-Container-Meta-Access-Expose-Headers: etag location x-timestamp x-trans-id Access-Control-Allow-Origin' \
https://storage101.containerRegion.clouddrive.com/v1/MossoCloudFS_yourAccoountNumber/yourContainer
```

## Browser Compatibility

Currently the uploader uses `XMLHttpRequest 2` to upload the files, which is not
Expand Down
3 changes: 2 additions & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Package.on_use(function (api) {
"lib/directive.js",
"lib/storage-policy.js",
"services/aws-s3.js",
"services/google-cloud.js"
"services/google-cloud.js",
"services/rackspace.js"
], "server");

api.export("Slingshot");
Expand Down
113 changes: 113 additions & 0 deletions services/rackspace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Slingshot.RackspaceFiles = {

directiveMatch: {
RackspaceAccountId: String,
RackspaceMetaDataKey: String,
container: String,
region: String,
pathPrefix: Match.OneOf(String, Function),
expire: Match.Where(function (expire) {
check(expire, Number);

return expire > 0;
}),
deleteAt: Match.Optional(Date),
deleteAfter: Match.Optional(Number)
},

directiveDefault: _.chain(Meteor.settings)
.pick("RackspaceAccountId", "RackspaceMetaDataKey")
.extend({
region: "iad3",
expire: 5 * 60 * 1000 //in 5 minutes
})
.value(),

version: "v1",

path: function (directive, prefix) {
return "/" + [
this.version,
"MossoCloudFS_" + directive.RackspaceAccountId,
directive.container,
prefix
].join("/").replace(/\/+/, "/");
},

pathPrefix: function (method, directive, file, meta) {
if ("pathPrefix" in directive) {
return (_.isFunction(directive.pathPrefix) ?
directive.pathPrefix.call(method, file, meta) : directive.pathPrefix);
}
else {
return "";
}
},

host: function (region) {
return "https://storage101." + region + ".clouddrive.com";
},

maxSize: 0x140000000, //5GB

upload: function (method, directive, file, meta) {
var pathPrefix = this.pathPrefix(method, directive, file, meta),
path = this.path(directive, pathPrefix),
host = this.host(directive.region),
url = host + path,
data = [
{
name: "redirect",
value: ""
},
{
name: "max_file_size",
value: Math.min(file.size, directive.maxSize || this.maxSize)
},
{
name: "max_file_count",
value: 1
},
{
name: "expires",
value: Date.now() + directive.expire
}
];

data.push({
name: "signature",
value: this.sign(directive.RackspaceMetaDataKey, path, data)
});

if ("deleteAt" in directive)
data.push({
name: "x_delete_at",
value: directive.deleteAt.getTime()
});

if ("deleteAfter" in directive)
data.push({
name: "x_delete_after",
value: Math.round(directive.deleteAfter / 1000)
});

var cdn = directive.cdn;

return {
upload: url,
download: (cdn && cdn + "/" + pathPrefix || host + path) + file.name,
postData: data
};
},

sign: function (secretkey, path, data) {
/* global Buffer: false */
var policy = path + "\n" + _.pluck(data, "value").join("\n");

return Npm.require("crypto")
.createHmac("sha1", secretkey)
.update(new Buffer(policy, "utf-8"))
.digest("hex");
}

};