Skip to content

Commit

Permalink
feat(sdk): addfile() is a preflight method which adds file from abs…
Browse files Browse the repository at this point in the history
…olute path (#3801)

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
0018akhil authored Aug 21, 2023
1 parent 1fa4e97 commit ba6730e
Show file tree
Hide file tree
Showing 23 changed files with 1,602 additions and 8 deletions.
202 changes: 202 additions & 0 deletions apps/jsii-docgen/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions apps/wing-api-checker/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/wing-api-checker/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions docs/docs/04-standard-library/01-cloud/bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ new cloud.Bucket(props?: BucketProps);

| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.cloud.Bucket.addFile">addFile</a></code> | Add a file to the bucket from system folder. |
| <code><a href="#@winglang/sdk.cloud.Bucket.addObject">addObject</a></code> | Add a file to the bucket that is uploaded when the app is deployed. |
| <code><a href="#@winglang/sdk.cloud.Bucket.onCreate">onCreate</a></code> | Run an inflight whenever a file is uploaded to the bucket. |
| <code><a href="#@winglang/sdk.cloud.Bucket.onDelete">onDelete</a></code> | Run an inflight whenever a file is deleted from the bucket. |
Expand All @@ -174,6 +175,40 @@ new cloud.Bucket(props?: BucketProps);

---

##### `addFile` <a name="addFile" id="@winglang/sdk.cloud.Bucket.addFile"></a>

```wing
addFile(key: str, path: str, encoding?: str): void
```

Add a file to the bucket from system folder.

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.cloud.Bucket.addFile.parameter.key"></a>

- *Type:* str

The key or name to associate with the file.

---

###### `path`<sup>Required</sup> <a name="path" id="@winglang/sdk.cloud.Bucket.addFile.parameter.path"></a>

- *Type:* str

The path to the file on the local system.

---

###### `encoding`<sup>Optional</sup> <a name="encoding" id="@winglang/sdk.cloud.Bucket.addFile.parameter.encoding"></a>

- *Type:* str

The encoding to use when reading the file.

Defaults to "utf-8".

---

##### `addObject` <a name="addObject" id="@winglang/sdk.cloud.Bucket.addObject"></a>

```wing
Expand Down
12 changes: 12 additions & 0 deletions examples/tests/sdk_tests/bucket/add_file.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;

let b = new cloud.Bucket();

b.addFile("file1.txt", "testFiles/test1.txt");
b.addFile("file2.txt", "testFiles/test2.txt");

test "addObject" {
assert(b.list().length == 2);
assert(b.get("file1.txt") == "test1");
assert(b.get("file2.txt") == "test2");
}
1 change: 1 addition & 0 deletions examples/tests/sdk_tests/bucket/testFiles/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions examples/tests/sdk_tests/bucket/testFiles/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
30 changes: 30 additions & 0 deletions libs/wingsdk/src/cloud/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from "fs";
import { isAbsolute, resolve } from "path";
import { Construct } from "constructs";
import { Topic } from "./topic";
import { fqnForType } from "../constants";
Expand Down Expand Up @@ -41,12 +43,14 @@ export abstract class Bucket extends Resource {

/** @internal */
protected readonly _topics = new Map<BucketEventType, Topic>();
private scope: Construct;

constructor(scope: Construct, id: string, props: BucketProps = {}) {
super(scope, id);

this.display.title = "Bucket";
this.display.description = "A cloud object store";
this.scope = scope;

this._addInflightOps(
BucketInflightMethods.DELETE,
Expand All @@ -73,6 +77,32 @@ export abstract class Bucket extends Resource {
*/
public abstract addObject(key: string, body: string): void;

/**
* Add a file to the bucket from system folder
*
* @param {string} key - The key or name to associate with the file.
* @param {string} path - The path to the file on the local system.
* @param {BufferEncoding} encoding - The encoding to use when reading the file. Defaults to "utf-8".
*/

public addFile(
key: string,
path: string,
encoding: BufferEncoding = "utf-8"
): void {
if (isAbsolute(path)) {
path = path;
} else {
if (!App.of(this.scope).entrypointDir) {
throw new Error("Missing environment variable: WING_SOURCE_DIR");
}
path = resolve(App.of(this.scope).entrypointDir, path);
}
const data = fs.readFileSync(path, { encoding: encoding });

this.addObject(key, data);
}

/**
* Creates a topic for subscribing to notification events
* @param actionType
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/test/sim-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SimApp extends sim.App {
private functionIndex: number = 0;

constructor() {
super({ outdir: mkdtemp() });
super({ outdir: mkdtemp(), entrypointDir: __dirname });

// symlink the node_modules so we can test imports and stuffs
fs.symlinkSync(
Expand Down
Loading

0 comments on commit ba6730e

Please sign in to comment.